Python[8] :paramiko模块多进程批量管理主机

本文介绍Paramiko模块的基础使用,包括安装、命令参数详解,并通过两个脚本实例展示如何批量执行命令及上传文件。

今天和大家聊聊paramiko模块


一、paramiko简介

  • paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。

  • paramiko主要是通过ssh协议对远程主机进行管理:包括执行远程主机CLI上传下载文件等。


二、快速安装paramiko模块

pip的详细安装请参考我的另外一篇文章:http://467754239.blog.51cto.com/4878013/1613612

1
2
# yum install python-devel
# pip install paramiko


三、paramiko命令参数详解


  • 利用密码登陆方式批量执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
导入模块
import  paramiko
 
实例化一个SSHClient对象
=  paramiko.SSHClient()
 
自动添加策略   #首次登陆用交互式确定(允许连接不在know_hosts文件中的主机)
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
要连接的主机地址信息
s.connect(hostname = 'ip地址' , port = 端口号, username = '用户名' , password = '密码' )
 
要执行的命令
stdin, stdout, stderr  =  s.exec_command( '执行的命令' )
 
查看命令的执行结果
print  stdout.read()
  • 利用公钥验证方式批量执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
首先建立公钥和私钥文件
# ssh-keygen
     
上传本地的公钥到远程主机的.ssh / authorized_keys文件中
# ssh-copy-id -i .ssh/id_rsa.pub root@ip
     
导入模块
import  paramiko
 
实例化一个对象
=  paramiko.SSHClient()
 
自动添加策略   #首次登陆用交互式确定
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     
建立私钥文件连接
key  =  paramiko.RSAKey.from_private_key_file( '/root/.ssh/id_rsa' )
     
通过私钥验证的方式登录远程主机
s.connect(hostname = 'ip地址' , port = 端口号, username = '用户名' , pkey = key)
     
要执行的命令
stdin, stdout, stderr  =  s.exec_command( '执行的命令' )
 
查看命令的执行结果
print  stdout.read()
  • 从远程主机上传、下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
导入模块
import  paramiko
 
#建立一个加密的传输管道
=  paramiko.Transport(( 'ip地址' ,端口号))
 
#建立连接
s.connect(username = '用户名' ,password = '密码' )
 
#建立一个sftp客户端对象,通过ssh transport操作远程文件
sftp  =  paramiko.SFTPClient.from_transport(s)
 
#上传本地文件到远程主机
sftp.put(localFile,remoteFile)
 
#从远程主机下载文件到本地
sftp.get(remoteFile,localFile)
 
#关闭sftp连接
s.close()


四、脚本演示

  • 局域网物理主机批量管理:执行命令

脚本还有很多瑕疵,只是作为参考练习,前提是所有主机的用户名、密码和端口号都要相同

例如:Username:root    Password:redhat     Port:22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@python script] # cat 02_paramiko_process.py 
#!/usr/bin/env python
#coding:utf8
 
from  multiprocessing  import  Process
import  paramiko
import  sys
 
Username  =  'root'
Password  =  'redhat'
Port  =  22
 
def  runCmd(ip,cmd):
     =  paramiko.SSHClient()
     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     try :
         s.connect(hostname = ip, port = Port, username = Username, password = Password)
         stdin, stdout, stderr  =  s.exec_command(cmd)
         result  =   stdout.read()
     print  ip,result,
     except :
     print  '%s is not exists'  %  ip
 
def  ipProcess():
     try :
         cmd  =  sys.argv[ 1 ]
         for  in  range ( 2 , 255 ):
             ip  =  '192.168.1.%s'  %  i
         =  Process(target = runCmd,args = (ip,cmd))
             p.start()
     except  IndexError:
         print  'please input a command.'
     
if  __name__  = =  '__main__' :
     ipProcess()

执行脚本:

1
[root@python script] # python 02_paramiko_process.py uptime

截图部分返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
192.168 . 1.57   10 : 48 : 49  up   2 : 29 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.56   10 : 50 : 25  up   2 : 29 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.100   10 : 48 : 50  up   2 : 29 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.127  is  not  exists
192.168 . 1.210   22 : 48 : 51  up   2 : 28 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.51   10 : 48 : 52  up   2 : 28 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.55   10 : 50 : 26  up   2 : 29 ,   0  users,  load average:  0.00 0.00 0.00
192.168 . 1.122  is  not  exists
192.168 . 1.58  is  not  exists
192.168 . 1.52  is  not  exists
192.168 . 1.53  is  not  exists
192.168 . 1.50  is  not  exists
192.168 . 1.215   02 : 31 : 14  up   2 : 07 ,   4  users,  load average:  4.75 1.46 0.82
  • 局域网物理主机批量管理:执行上传、下载文件

这里只对批量上传做了演示,对于批量下载文件只是一行代码的更换就可以了,我也在脚本中添加了注释行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@python script] # cat 03_put_paramiko_process.py 
#!/usr/bin/env python
#coding:utf8
 
from  multiprocessing  import  Process
import  paramiko
import  sys
 
Username  =  'root'
Password  =  'redhat'
Port  =  22
 
def  sftpPut(ip):
     try :
         =  paramiko.Transport((ip,Port))
         s.connect(username = Username,password = Password)
         sftp  =  paramiko.SFTPClient.from_transport(s)
         localFile  =  '/root/sync.sh'
         remoteFile  =  '/opt/sync.sh'
         sftp.put(localFile,remoteFile)
         #sftp.get(remoteFile,localFile)
         s.close()
     print  '%s put successful.'  %  ip
     except :
     print  '%s not exists.'  %  ip
 
def  ipProcess():
     for  in  range ( 2 , 255 ):
         ip  =  '192.168.1.%s'  %  i
         =  Process(target = sftpPut,args = (ip,))
         p.start()
     
if  __name__  = =  '__main__' :
     ipProcess()

执行脚本:

1
[root@python script] # python 03_put_paramiko_process.py

截图部分返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
192.168 . 1.55  put successful.
192.168 . 1.181  not  exists.
192.168 . 1.198  not  exists.
192.168 . 1.200  not  exists.
192.168 . 1.207  not  exists.
192.168 . 1.209  not  exists.
192.168 . 1.57  put successful.
192.168 . 1.51  put successful.
192.168 . 1.56  put successful.
192.168 . 1.233  not  exists.
192.168 . 1.127  not  exists.
192.168 . 1.100  put successful.
192.168 . 1.210  put successful.

登陆远程主机查看文件是否上传完成:

1
2
3
4
5
6
7
8
[root@python script] # ssh 192.168.1.51
Address  192.168 . 1.51  maps to localhost, but this does  not  map  back to the address  -  POSSIBLE BREAK - IN ATTEMPT!
root@ 192.168 . 1.51 's password: 
Last login: Wed Mar  11  11 : 04 : 30  2015  from  192.168 . 1.112
[root@naginx ~] # ifconfig |grep inet |head -1
           inet addr: 192.168 . 1.51   Bcast: 192.168 . 1.255   Mask: 255.255 . 255.0
[root@naginx ~] # ll /opt/sync.sh -d
- rw - r - - r - -  1  root root  333  3 月   11  11 : 05  / opt / sync.sh     #已经成功上传文件


  • 注意:

  • 批量上传文件脚本只能满足单个文件,如何实现多个文件上传和下载呢?可以通过os模块来实现,下一篇文章中介绍如何以更人性化的方式实现参数上传下载,那就是os模块和optparse模块

多文件上传、下载请参考此文章:http://467754239.blog.51cto.com/4878013/1619323





     本文转自zys467754239 51CTO博客,原文链接:http://blog.51cto.com/467754239/1619166,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值