python下paramiko模块学习之二(利用配置文件登录批量主机)
之前我写过一篇关于python中paramiko模块简单功能的文章,今天继续给大家介绍这个模块的一些用法。
今天主要是利用python读取配置文件来登录批量主机,并在主机上执行shell命令,废话不说了,直接上代码了,大家可以看看:
- #!/usr/bin/env python
- import paramiko
- import os
- import datetime
- from ConfigParser import ConfigParser
- ConfigFile='config.ini'
- config=ConfigParser()
- config.read(ConfigFile)
- hostname1=''.join(config.get('IP','ipaddress'))
- address=hostname1.split('; ')
- print address
- username='root'
- password='abc123'
- port=22
- local_dir='/tmp/'
- remote_dir='/tmp/test/'
- if __name__=="__main__":
- for ip in address:
- paramiko.util.log_to_file('paramiko.log')
- s=paramiko.SSHClient()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(hostname=ip,username=username,password=password)
- stdin,stdout,stderr=s.exec_command('free; ifconfig; df -h')
- print stdout.read()
- s.close()
- [IP]
- ipaddress = 74.63.229.*; 69.50.220.*
- [root@centos6 python]# clear
- [root@centos6 python]# python paramiko-config.py
- ['74.63.229.*', '69.50.220.*']
- totalusedfreesharedbufferscached
- Mem:39321622308370908000
- -/+ buffers/cache:22308370908
- Swap:000
- loLink encap:Local Loopback
- inet addr:127.0.0.1Mask:255.0.0.0
- inet6 addr: ::1/128 Scope:Host
- UP LOOPBACK RUNNINGMTU:16436Metric:1
- RX packets:14 errors:0 dropped:0 overruns:0 frame:0
- TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:956 (956.0 B)TX bytes:956 (956.0 B)
- venet0Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
- UP BROADCAST POINTOPOINT RUNNING NOARPMTU:1500Metric:1
- RX packets:36498 errors:0 dropped:0 overruns:0 frame:0
- TX packets:36433 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:8698019 (8.2 MiB)TX bytes:5322427 (5.0 MiB)
- venet0:0Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
- inet addr:74.63.229.*P-t-P:74.63.229.56Bcast:0.0.0.0Mask:255.255.255.255
- UP BROADCAST POINTOPOINT RUNNING NOARPMTU:1500Metric:1
- FilesystemSizeUsed Avail Use% Mounted on
- /dev/simfs10G408M9.7G4% /
- tmpfs192M0192M0% /lib/init/rw
- tmpfs192M0192M0% /dev/shm
- totalusedfreesharedbufferscached
- Mem:26214415412010802405094862668
- -/+ buffers/cache:40504221640
- Swap:2621360262136
- eth0Link encap:EthernetHWaddr 00:16:3E:27:61:01
- inet addr:69.50.220.*Bcast:69.50.223.255Mask:255.255.240.0
- UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
- RX packets:43755717 errors:0 dropped:0 overruns:0 frame:0
- TX packets:79002 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:3003027220 (2.7 GiB)TX bytes:39705224 (37.8 MiB)
- loLink encap:Local Loopback
- inet addr:127.0.0.1Mask:255.0.0.0
- UP LOOPBACK RUNNINGMTU:16436Metric:1
- RX packets:0 errors:0 dropped:0 overruns:0 frame:0
- TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:0 (0.0 b)TX bytes:0 (0.0 b)
- FilesystemSizeUsed Avail Use% Mounted on
- /dev/sda115G4.7G9.4G33% /
- none128M0128M0% /dev/shm
- [root@centos6 python]#
这个例子这个例子十分简单,大家可以扩张下,在运维工作中,我们管理的机器可不止2台吧,你就可以你管理的服务器IP都写在配置文件里,中间又分号隔开就可以了,还有一点就是你可能执行的命令也有很多,其实也可以写在配置文件里,而不必像我这样写死在代码里面,其他扩展功能大家可以自己去看看
【python下paramiko模块学习之二(利用配置文件登录批量主机)】
推荐阅读
- 开学第一天(下)
- 【故障公告】周五下午的一次突发故障
- 生活随笔|好天气下的意外之喜
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 汇讲-勇于突破
- python学习之|python学习之 实现QQ自动发送消息
- Android中的AES加密-下
- 说的真好
- 逻辑回归的理解与python示例
- 放下心中的偶像包袱吧