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] 
  2.  

 
  1. ipaddress = 74.63.229.*;69.50.220.* 
 
  1.  

下面给大家看下效果:

 

 
  1. [root@centos6 python]# clear 
  2. [root@centos6 python]# python paramiko-config.py 
  3. ['74.63.229.*''69.50.220.*'
  4.              total       used       free     shared    buffers     cached 
  5. Mem:        393216      22308     370908          0          0          0 
  6. -/+ buffers/cache:      22308     370908 
  7. Swap:            0          0          0 
  8. lo        Link encap:Local Loopback 
  9.           inet addr:127.0.0.1  Mask:255.0.0.0 
  10.           inet6 addr: ::1/128 Scope:Host 
  11.           UP LOOPBACK RUNNING  MTU:16436  Metric: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.  
  17. venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 
  18.           UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1 
  19.           RX packets:36498 errors:0 dropped:0 overruns:0 frame:0 
  20.           TX packets:36433 errors:0 dropped:0 overruns:0 carrier:0 
  21.           collisions:0 txqueuelen:0 
  22.           RX bytes:8698019 (8.2 MiB)  TX bytes:5322427 (5.0 MiB) 
  23.  
  24. venet0:0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 
  25.           inet addr:74.63.229.*  P-t-P:74.63.229.56  Bcast:0.0.0.0  Mask:255.255.255.255 
  26.           UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1 
  27.  
  28. Filesystem            Size  Used Avail Use% Mounted on 
  29. /dev/simfs             10G  408M  9.7G   4% / 
  30. tmpfs                 192M     0  192M   0% /lib/init/rw 
  31. tmpfs                 192M     0  192M   0% /dev/shm 
  32.  
  33.              total       used       free     shared    buffers     cached 
  34. Mem:        262144     154120     108024          0      50948      62668 
  35. -/+ buffers/cache:      40504     221640 
  36. Swap:       262136          0     262136 
  37. eth0      Link encap:Ethernet  HWaddr 00:16:3E:27:61:01 
  38.           inet addr:69.50.220.*  Bcast:69.50.223.255  Mask:255.255.240.0 
  39.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
  40.           RX packets:43755717 errors:0 dropped:0 overruns:0 frame:0 
  41.           TX packets:79002 errors:0 dropped:0 overruns:0 carrier:0 
  42.           collisions:0 txqueuelen:1000 
  43.           RX bytes:3003027220 (2.7 GiB)  TX bytes:39705224 (37.8 MiB) 
  44.  
  45. lo        Link encap:Local Loopback 
  46.           inet addr:127.0.0.1  Mask:255.0.0.0 
  47.           UP LOOPBACK RUNNING  MTU:16436  Metric:1 
  48.           RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
  49.           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
  50.           collisions:0 txqueuelen:0 
  51.           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b) 
  52.  
  53. Filesystem            Size  Used Avail Use% Mounted on 
  54. /dev/sda1              15G  4.7G  9.4G  33% / 
  55. none                  128M     0  128M   0% /dev/shm 
  56.  
  57. [root@centos6 python]# 

   呵呵,代码十分简单,我在linux虚拟机下执行没有问题,上面的2个实体ip是我的2个VPS,登录和执行都很快,但是在window下却报错,比较困惑,也没有深究,希望有知道的同仁指点下。

    这个例子这个例子十分简单,大家可以扩张下,在运维工作中,我们管理的机器可不止2台吧,你就可以你管理的服务器IP都写在配置文件里,中间又分号隔开就可以了,还有一点就是你可能执行的命令也有很多,其实也可以写在配置文件里,而不必像我这样写死在代码里面,其他扩展功能大家可以自己去看看

本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/706999如需转载请自行联系原作者


world77

相关文章
|
5天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
10 1
|
1天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
1天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
1天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
1天前
|
存储 索引 Python
python学习5-列表的创建、增删改查、排序
python学习5-列表的创建、增删改查、排序
|
1天前
|
Python
python学习3-选择结构、bool值、pass语句
python学习3-选择结构、bool值、pass语句
|
1天前
|
Python
Python学习的最佳资源是什么?
【4月更文挑战第15天】Python学习的最佳资源是什么?
15 7
|
2天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
3月前
|
Python
python基础学习 -- 函数
python基础学习 -- 函数
24 0
|
Python
python基础学习 -- 函数
python基础学习 -- 函数
83 0