python下paramiko模块学习之二(利用配置文件登录批量主机)

之前我写过一篇关于python中paramiko模块简单功能的文章,今天继续给大家介绍这个模块的一些用法。
今天主要是利用python读取配置文件来登录批量主机,并在主机上执行shell命令,废话不说了,直接上代码了,大家可以看看:


  1. #!/usr/bin/env python
  2. import paramiko
  3. import os
  4. import datetime
  5. from ConfigParser import ConfigParser
  6. ConfigFile='config.ini'
  7. config=ConfigParser()
  8. config.read(ConfigFile)
  9. hostname1=''.join(config.get('IP','ipaddress'))
  10. address=hostname1.split('; ')
  11. print address
  12. username='root'
  13. password='abc123'
  14. port=22
  15. local_dir='/tmp/'
  16. remote_dir='/tmp/test/'
  17. if __name__=="__main__":
  18. for ip in address:
  19. paramiko.util.log_to_file('paramiko.log')
  20. s=paramiko.SSHClient()
  21. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  22. s.connect(hostname=ip,username=username,password=password)
  23. stdin,stdout,stderr=s.exec_command('free; ifconfig; df -h')
  24. print stdout.read()
  25. s.close()
下面再贴上config.ini配置文件内容:

  1. [IP]


  1. ipaddress = 74.63.229.*; 69.50.220.*

下面给大家看下效果:


  1. [root@centos6 python]# clear
  2. [root@centos6 python]# python paramiko-config.py
  3. ['74.63.229.*', '69.50.220.*']
  4. totalusedfreesharedbufferscached
  5. Mem:39321622308370908000
  6. -/+ buffers/cache:22308370908
  7. Swap:000
  8. loLink encap:Local Loopback
  9. inet addr:127.0.0.1Mask:255.0.0.0
  10. inet6 addr: ::1/128 Scope:Host
  11. UP LOOPBACK RUNNINGMTU:16436Metric:1
  12. RX packets:14 errors:0 dropped:0 overruns:0 frame:0
  13. TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
  14. collisions:0 txqueuelen:0
  15. RX bytes:956 (956.0 B)TX bytes:956 (956.0 B)
  16. venet0Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  17. UP BROADCAST POINTOPOINT RUNNING NOARPMTU:1500Metric:1
  18. RX packets:36498 errors:0 dropped:0 overruns:0 frame:0
  19. TX packets:36433 errors:0 dropped:0 overruns:0 carrier:0
  20. collisions:0 txqueuelen:0
  21. RX bytes:8698019 (8.2 MiB)TX bytes:5322427 (5.0 MiB)
  22. venet0:0Link encap:UNSPECHWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
  23. inet addr:74.63.229.*P-t-P:74.63.229.56Bcast:0.0.0.0Mask:255.255.255.255
  24. UP BROADCAST POINTOPOINT RUNNING NOARPMTU:1500Metric:1
  25. FilesystemSizeUsed Avail Use% Mounted on
  26. /dev/simfs10G408M9.7G4% /
  27. tmpfs192M0192M0% /lib/init/rw
  28. tmpfs192M0192M0% /dev/shm
  29. totalusedfreesharedbufferscached
  30. Mem:26214415412010802405094862668
  31. -/+ buffers/cache:40504221640
  32. Swap:2621360262136
  33. eth0Link encap:EthernetHWaddr 00:16:3E:27:61:01
  34. inet addr:69.50.220.*Bcast:69.50.223.255Mask:255.255.240.0
  35. UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  36. RX packets:43755717 errors:0 dropped:0 overruns:0 frame:0
  37. TX packets:79002 errors:0 dropped:0 overruns:0 carrier:0
  38. collisions:0 txqueuelen:1000
  39. RX bytes:3003027220 (2.7 GiB)TX bytes:39705224 (37.8 MiB)
  40. loLink encap:Local Loopback
  41. inet addr:127.0.0.1Mask:255.0.0.0
  42. UP LOOPBACK RUNNINGMTU:16436Metric:1
  43. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  44. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  45. collisions:0 txqueuelen:0
  46. RX bytes:0 (0.0 b)TX bytes:0 (0.0 b)
  47. FilesystemSizeUsed Avail Use% Mounted on
  48. /dev/sda115G4.7G9.4G33% /
  49. none128M0128M0% /dev/shm
  50. [root@centos6 python]#
呵呵,代码十分简单,我在linux虚拟机下执行没有问题,上面的2个实体ip是我的2个VPS,登录和执行都很快,但是在window下却报错,比较困惑,也没有深究,希望有知道的同仁指点下。
这个例子这个例子十分简单,大家可以扩张下,在运维工作中,我们管理的机器可不止2台吧,你就可以你管理的服务器IP都写在配置文件里,中间又分号隔开就可以了,还有一点就是你可能执行的命令也有很多,其实也可以写在配置文件里,而不必像我这样写死在代码里面,其他扩展功能大家可以自己去看看
【python下paramiko模块学习之二(利用配置文件登录批量主机)】

    推荐阅读