首页 文章 精选 留言 我的

精选列表

搜索[子系统命令],共10000篇文章
优秀的个人博客,低调大师

利用paramiko模块批量登录(执行命令/上传文件)

工作中由于服务器主机很多,如果手动的一台一台去添加ssh认证,效率太低了,而此脚本正是为了解决此问题 此脚本的实现的功能: 1、实现了(密码、ssh认证)单一主机登录和批量主机登录 2、实现了(密码、ssh认证)单一主机上传文件和批量主机上传文件(下载文件的原理和此一样) 3、主机批量添加ssh认证(这才是我的主要目的) 脚本的不足: 1、只能循环主机名 2、所有的主机的账号和密码都是一样的,不够灵活 有需求的朋友可以修改下代码,可以把主机、账号密码存放在一个文件中,循环读取文件 下面贴上代码吧 如下代码有错 欢迎各位纠正 有错才有进步 你们的指点是我进步的源泉 #!/usr/bin/python #coding:utf-8 #需要安装paramiko模块 #yuminstallpython-paramiko-y importparamiko,os classSSH2: def__init__(self,hostname=None,username=None,password=None,key_file=None,port=22,timeout=30): self.h=hostname self.u=username self.p=password self.t=timeout self.port=int(port) self.key_file=key_file #使用密码登录 defssh2_pass(self,command=None): paramiko.util.log_to_file('/tmp/paramiko.log') ssh2=paramiko.SSHClient() ssh2.load_system_host_keys() ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh2.connect(hostname=self.h,username=self.u,password=self.p,port=self.port,timeout=self.t) stdin,stdout,stderr=ssh2.exec_command(command) print'{0}\t{1}'.format(self.h,command) printstdout.read() ssh2.close() #使用key密钥登录 defssh2_key(self,command=None): paramiko.util.log_to_file('/tmp/paramiko.log') key=paramiko.RSAKey.from_private_key_file(self.key_file) ssh2=paramiko.SSHClient() ssh2.load_system_host_keys() ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh2.connect(hostname=self.h,username=self.u,pkey=key,port=self.port,timeout=self.t) stdin,stdout,stderr=ssh2.exec_command(command) print'{0}\t{1}'.format(self.h,command) printstdout.read() ssh2.close() #使用密码上传文件 defsftp_pass_put(self,localpath=None,remotepath=None): t=paramiko.Transport((self.h,self.port)) t.connect(username=self.u,password=self.p) sftp=paramiko.SFTPClient.from_transport(t) sftp.put(localpath=localpath,remotepath=remotepath) printself.h,localpath t.close() #使用key密钥上传文件 defsftp_key_put(self,localpath=None,remotepath=None): key=paramiko.RSAKey.from_private_key_file(self.key_file) t=paramiko.Transport((self.h,self.port)) t.connect(username=self.u,pkey=key) sftp=paramiko.SFTPClient.from_transport(t) sftp.put(localpath=localpath,remotepath=remotepath) printself.h,localpath t.close() if__name__=='__main__': ###################使用密码登录主机############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #SSH=SSH2(hostname=hostname,username=username,password=password,port=22) #SSH.ssh2_pass(command='df-h') ###################使用密钥登录主机############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #SSH=SSH2(hostname=hostname,username=username,key_file=key_file,port=22) #SSH.ssh2_pass(command='df-h') ###################使用密码上传文件############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #本地文件绝对路径 #密钥需要自己ssh-keygen生成 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #SSH=SSH2(hostname=hostname,username=username,password=password,port=22) #SSH.sftp_pass_put(localpath=local,remotepath=remote) #SSH.ssh2_pass(command='ls-l{0}'.format(remote)) ###################使用密钥上传文件############################################################# #远程服务器IP地址 #hostname='192.168.31.102' #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #SSH=SSH2(hostname=hostname,username=username,port=22,key_file=key_file) #SSH.sftp_key_put(localpath=local,remotepath=remote) #SSH.ssh2_key(command='ls-l{0}'.format(remote)) #####################使用密码批量添加用户认证############################################################ #hostname=['192.168.31.101','192.168.31.102','192.168.31.103'] #远程服务器的账号和密码 #username='xiaomo' #password='xiaomo' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #批量上传密钥文件并添加认证 #如果.ssh目录或authorized_keys文件事先存在,请确认权限是否正确 #forhinhostname: #SSH=SSH2(hostname=h,username=username,password=password,port=22,timeout=30) ##/home/xiaomo/.ssh/是远程服务器的用户目录 #SSH.ssh2_pass(command='(umask077;test-d/home/xiaomo/.ssh||mkdir/home/xiaomo/.ssh)') #SSH.sftp_pass_put(localpath=local,remotepath=remote) #SSH.ssh2_pass(command='(umask077;cat{0}>>/home/xiaomo/.ssh/authorized_keys&&rm-f{0})'.format(remote)) #SSH.ssh2_pass(command='cat/home/xiaomo/.ssh/authorized_keys') ##################使用密钥批量上传文件################################################################### #hostname=['192.168.31.101','192.168.31.102','192.168.31.103'] #远程服务器的账号 #username='xiaomo' #指定密钥文件的位置 #key_file='/home/xiaomo/.ssh/id_rsa' #本地文件绝对路径 #local='/home/xiaomo/.ssh/id_rsa.pub' #远程服务器文件绝对路径 #remote='/tmp/id_rsa.pub' #forhinhostname: #SSH=SSH2(hostname=h,username=username,port=22,key_file=key_file) #SSH.sftp_key_put(localpath=local,remotepath=remote) #SSH.ssh2_key(command='ls-l{0}'.format(remote)) #############################################################################################

优秀的个人博客,低调大师

openstack 命令行管理十九 - 日志管理 (备忘)

instance 终端以日志方式输出方法 [root@station140 ~(network_admin)]# nova console-log --length 80 terry_instance1 | tail Starting atd: [ OK ][ OK ] Starting yum-updatesd: [ OK ] Failed to retrieve hostname from instance metadata. This is a soft error so we'll continue Failed to retrieve user-data from instance metadata. This is a soft error so we'll continue Starting smartd: [ OK ] CentOS release 5.8 (Final) Kernel 2.6.18-308.el5 on an x86_64 host-10-0-0-50 login:

优秀的个人博客,低调大师

openstack 命令行管理十四 - 路由管理 (备忘)

 帮助 [root@station140 ~(network_admin)]# neutron help | grep route l3-agent-list-hosting-router List L3 agents hosting a router. l3-agent-router-add Add a router to a L3 agent. l3-agent-router-remove Remove a router from a L3 agent. net-gateway-connect Add an internal network interface to a router. router-create Create a router for a given tenant. router-delete Delete a given router. router-gateway-clear Remove an external network gateway from a router. router-gateway-set Set the external network gateway for a router. router-interface-add Add an internal network interface to a router. router-interface-delete Remove an internal network interface from a router. router-list List routers that belong to a given tenant. router-list-on-l3-agent List the routers on a L3 agent. router-port-list List ports that belong to a given tenant, with specified router. router-show Show information of a given router. router-update Update router's information. 查询 tenant [root@station140 ~(network_admin)]# keystone tenant-list +----------------------------------+----------+---------+ | id | name | enabled | +----------------------------------+----------+---------+ | 9467f30b8bba4770a06a687e4584636b | cloud | True | +----------------------------------+----------+---------+ 创建路由连接到外部网络, 这个路由可以与内部网络进行连接 你可以创建过程中指定一个 tenant, 利用参数 --tenant-id 9467f30b8bba4770a06a687e4584636b 进行定义 创建路由 neutron router-create ext-to-int --tenant-id 9467f30b8bba4770a06a687e4584636b Created a new router: +-----------------------+--------------------------------------+ | Field | Value | +-----------------------+--------------------------------------+ | admin_state_up | True | | external_gateway_info | | | id | b83f43cd-bf8f-42f8-812a-708c2c372820 | | name | ext-to-int | | status | ACTIVE | | tenant_id | 9467f30b8bba4770a06a687e4584636b | +-----------------------+--------------------------------------+ 查询方法 [root@station140 ~(keystone_admin)]# neutron router-list | grep -v router1 +--------------------------------------+------------+------------------------+ | id | name | external_gateway_info | +--------------------------------------+------------+------------------------+ | b83f43cd-bf8f-42f8-812a-708c2c372820 | ext-to-int | null | +--------------------------------------+------------+------------------------+ 查询外部网络 [root@station140 ~(keystone_admin)]# neutron net-list | grep pub1 +--------------------------------------+---------+------------------------------------------------------+ | id | name | subnets | +--------------------------------------+---------+------------------------------------------------------+ | aebe75f0-6013-4a5e-bbd9-cb81e1f017bc | pub1 | 59cc431b-498e-49a0-bc60-4a8ca1ca6f48 192.168.48.0/24 | +--------------------------------------+---------+------------------------------------------------------+ 连接路由到外部网络, 设定外部网络网关 # neutron router-gateway-set b83f43cd-bf8f-42f8-812a-708c2c372820 aebe75f0-6013-4a5e-bbd9-cb81e1f017bc [root@station140 ~(keystone_admin)]# neutron router-list | grep -v router1 +--------------------------------------+------------+-----------------------------------------------------------------------------+ | id | name | external_gateway_info | +--------------------------------------+------------+-----------------------------------------------------------------------------+ | b83f43cd-bf8f-42f8-812a-708c2c372820 | ext-to-int | {"network_id": "aebe75f0-6013-4a5e-bbd9-cb81e1f017bc", "enable_snat": true} | +--------------------------------------+------------+-----------------------------------------------------------------------------+ 列出子网信息 [root@station140 ~(keystone_admin)]# neutron subnet-list | grep terry +--------------------------------------+----------------+-----------------+------------------------------------------------------+ | id | name | cidr | allocation_pools | +--------------------------------------+----------------+-----------------+------------------------------------------------------+ | 3066c397-bccf-4473-8a94-72b09a97a70a | terry_pri_net1 | 10.0.0.0/8 | {"start": "10.0.0.50", "end": "10.0.0.100"} | | 59cc431b-498e-49a0-bc60-4a8ca1ca6f48 | terry_pub_net1 | 192.168.48.0/24 | {"start": "192.168.48.142", "end": "192.168.48.148"} | +--------------------------------------+----------------+-----------------+------------------------------------------------------+ 创建内部网络路由接口 [root@station140 ~(network_admin)]# neutron router-interface-add b83f43cd-bf8f-42f8-812a-708c2c372820 3066c397-bccf-4473-8a94-72b09a97a70a Added interface c9566299-44ed-4924-b845-4fc48bd4de98bbfe84a2-508e-47a1-a664-27b2e8121893 to router b83f43cd-bf8f-42f8-812a-708c2c372820. 显示路由信息 [root@station140 ~(keystone_admin)]# neutron router-show b83f43cd-bf8f-42f8-812a-708c2c372820 +-----------------------+-----------------------------------------------------------------------------+ | Field | Value | +-----------------------+-----------------------------------------------------------------------------+ | admin_state_up | True | | external_gateway_info | {"network_id": "aebe75f0-6013-4a5e-bbd9-cb81e1f017bc", "enable_snat": true} | | id | b83f43cd-bf8f-42f8-812a-708c2c372820 | | name | ext-to-int | | routes | | | status | ACTIVE | | tenant_id | 9467f30b8bba4770a06a687e4584636b | +-----------------------+-----------------------------------------------------------------------------+ 移除路由接口 (interface) [root@station140 ~(keystone_admin)]# neutron router-interface-delete b83f43cd-bf8f-42f8-812a-708c2c372820 3066c397-bccf-4473-8a94-72b09a97a70a Removed interface from router b83f43cd-bf8f-42f8-812a-708c2c372820 移除路由的默认网关 查询 [root@station140 ~(keystone_admin)]# neutron router-list | grep network | grep ext-to-int +--------------------------------------+------------+-----------------------------------------------------------------------------+ | id | name | external_gateway_info | +--------------------------------------+------------+-----------------------------------------------------------------------------+ | b83f43cd-bf8f-42f8-812a-708c2c372820 | ext-to-int | {"network_id": "aebe75f0-6013-4a5e-bbd9-cb81e1f017bc", "enable_snat": true} | +--------------------------------------+------------+-----------------------------------------------------------------------------+ 当 external_gateway_info 则表示具有默认网关 删除网关接口 [root@station140 ~(keystone_admin)]# neutron router-gateway-clear b83f43cd-bf8f-42f8-812a-708c2c372820 Removed gateway from router b83f43cd-bf8f-42f8-812a-708c2c372820 下面显示为不具备网关的路由 [root@station140 ~(keystone_admin)]# neutron router-list | grep ext-to-int +--------------------------------------+------------+-----------------------+ | id | name | external_gateway_info | +--------------------------------------+------------+-----------------------+ | b83f43cd-bf8f-42f8-812a-708c2c372820 | ext-to-int | null | +--------------------------------------+------------+-----------------------+ 删除路由 [root@station140 ~(keystone_admin)]# neutron router-delete b83f43cd-bf8f-42f8-812a-708c2c372820 Deleted router: b83f43cd-bf8f-42f8-812a-708c2c372820

优秀的个人博客,低调大师

openstack 命令行管理十六 - 网络测试 (备忘)

 注: 192.168.48.142 为 floating ip ping 测试 [root@station140 ~(keystone_admin)]# ping 192.168.48.142 PING 192.168.48.142 (192.168.48.142) 56(84) bytes of data. 64 bytes from 192.168.48.142: icmp_seq=1 ttl=63 time=1.61 ms ssh 测试 [root@station140 ~(keystone_admin)]# ssh 192.168.48.142 The authenticity of host '192.168.48.142 (192.168.48.142)' can't be established. RSA key fingerprint is 7f:cc:6e:22:e4:39:99:6a:4c:de:4a:82:df:e9:1c:6c. Are you sure you want to continue connecting (yes/no)? ip netns 空间测试方法 查询路由方法 [root@station140 ~(keystone_admin)]# neutron router-list | grep -v router1 +--------------------------------------+------------+-----------------------------------------------------------------------------+ | id | name | external_gateway_info | +--------------------------------------+------------+-----------------------------------------------------------------------------+ | b83f43cd-bf8f-42f8-812a-708c2c372820 | ext-to-int | {"network_id": "aebe75f0-6013-4a5e-bbd9-cb81e1f017bc", "enable_snat": true} | +--------------------------------------+------------+-----------------------------------------------------------------------------+ 列出网络命名空间 [root@station140 ~(keystone_admin)]# ip netns qrouter-59fa152c-df2e-43e4-9d09-7b89fb9cce5d qdhcp-dc121f2d-1b3b-471f-8fe7-15ef973b949e qrouter-99859660-68fe-4be4-835a-073a05da6533 qrouter-b83f43cd-bf8f-42f8-812a-708c2c372820 qdhcp-d0e3f988-d62f-4f95-ab21-b73f4dae326b qdhcp-e8e14001-44d9-4ab1-a462-ea621b8a4746 利用网络命名空间进行操作例 1 [root@station140 ~(keystone_admin)]# ip netns exec qrouter-b83f43cd-bf8f-42f8-812a-708c2c372820 ip addr list 32: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 33: qg-5a8b8f30-f5: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether fa:16:3e:6f:b7:1c brd ff:ff:ff:ff:ff:ff inet 192.168.48.143/24 brd 192.168.48.255 scope global qg-5a8b8f30-f5 inet 192.168.48.142/32 brd 192.168.48.142 scope global qg-5a8b8f30-f5 inet6 fe80::f816:3eff:fe6f:b71c/64 scope link valid_lft forever preferred_lft forever 34: qr-bbfe84a2-50: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether fa:16:3e:1c:fd:1e brd ff:ff:ff:ff:ff:ff inet 10.0.0.1/8 brd 10.255.255.255 scope global qr-bbfe84a2-50 inet6 fe80::f816:3eff:fe1c:fd1e/64 scope link valid_lft forever preferred_lft forever 利用网络命名空间进行操作例 2 [root@station140 ~(keystone_admin)]# ip netns exec qrouter-b83f43cd-bf8f-42f8-812a-708c2c372820 ping 192.168.48.142 PING 192.168.48.142 (192.168.48.142) 56(84) bytes of data. 64 bytes from 192.168.48.142: icmp_seq=1 ttl=64 time=2.01 ms ^C --- 192.168.48.142 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 615ms rtt min/avg/max/mdev = 2.013/2.013/2.013/0.000 ms 利用网络命名空间进行操作例 3 [root@station140 ~(keystone_admin)]# ip netns exec qrouter-b83f43cd-bf8f-42f8-812a-708c2c372820 ping 10.0.0.50 PING 10.0.0.50 (10.0.0.50) 56(84) bytes of data. 64 bytes from 10.0.0.50: icmp_seq=1 ttl=64 time=2.27 ms

优秀的个人博客,低调大师

openstack 命令行管理八 - 服务管理 (备忘)

 帮助 [root@station140 tmp(keystone_admin)]# keystone help | grep service service-create Add service to Service Catalog. service-delete Delete service from Service Catalog. service-get Display service from Service Catalog. service-list List all services in Service Catalog. 列出当前的服务方法 [root@station140 tmp(keystone_admin)]# keystone service-list +----------------------------------+------------+--------------+--------------------------------+ | id | name | type | description | +----------------------------------+------------+--------------+--------------------------------+ | 4db88c4e3efe4f188b6f08756d28c407 | ceilometer | metering | Openstack Metering Service | | e4ce5457b38e4a50a929af9b2b02b81c | cinder | volume | Cinder Service | | ab1e6db2a7b54a459f928075263a8b0f | cinder_v2 | volumev2 | Cinder Service v2 | | f025dc6a95db40f586b6975b154465b9 | glance | image | Openstack Image Service | | 04a34fae776c4573a4d26ccca9407ec9 | keystone | identity | OpenStack Identity Service | | 39f89e436d4942a1b2349e02cfddbed2 | neutron | network | Neutron Networking Service | | 60796518d55347f981ce319121c5e6bf | nova | compute | Openstack Compute Service | | 7867dcf1ec1647efa2287344c2f00775 | nova_ec2 | ec2 | EC2 Service | | 743203738a074f5bac198be5e52977d8 | swift | object-store | Openstack Object-Store Service | | 8b7b18fc6b7c4f169b10282252c0571c | swift_s3 | s3 | Openstack S3 Service | +----------------------------------+------------+--------------+--------------------------------+ 服务管理帮助 [root@station140 tmp(keystone_admin)]# nova help | grep service [--service-type <service-type>] [--service-name <service-name>] [--volume-service-name <volume-service-name>] authenticate services. host-list List all hosts by service. service-disable Disable the service. service-enable Enable the service. service-list Show a list of all running services. Filter by host & --service-type <service-type> --service-name <service-name> --volume-service-name <volume-service-name> 列出 openstack 当前可用的服务器 [root@station140 tmp(keystone_admin)]# nova host-list +----------------------+-------------+----------+ | host_name | service | zone | +----------------------+-------------+----------+ | station140.cloud.com | consoleauth | internal | | station140.cloud.com | scheduler | internal | | station140.cloud.com | conductor | internal | | station140.cloud.com | compute | nova | | station140.cloud.com | cert | internal | +----------------------+-------------+----------+注: 当前使用 all in one 模式, 所以只返回一个 host_name 结果 列出主机上的服务状态 [root@station140 tmp(keystone_admin)]# nova service-list +------------------+----------------------+----------+---------+-------+----------------------------+-----------------+ | Binary | Host | Zone | Status | State | Updated_at | Disabled Reason | +------------------+----------------------+----------+---------+-------+----------------------------+-----------------+ | nova-consoleauth | station140.cloud.com | internal | enabled | up | 2014-02-14T08:40:37.000000 | None | | nova-scheduler | station140.cloud.com | internal | enabled | up | 2014-02-14T08:40:37.000000 | None | | nova-conductor | station140.cloud.com | internal | enabled | up | 2014-02-14T08:40:38.000000 | None | | nova-compute | station140.cloud.com | nova | enabled | up | 2014-02-14T08:40:29.000000 | None | | nova-cert | station140.cloud.com | internal | enabled | up | 2014-02-14T08:40:33.000000 | None | +------------------+----------------------+----------+---------+-------+----------------------------+-----------------+ 关闭某个服务: nova service-disable localhost.localdomain nova-compute --reason 'trial log' <- just test 重新启动服务 nova service-enable localhost.localdomain nova-compute

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册