zoukankan      html  css  js  c++  java
  • paramiko模块使用

    paramiko简介

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

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

    安装paramiko模块

    python -m 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.000.000.00
    192.168.1.56  10:50:25 up  2:29,  0 users,  load average: 0.000.000.00
    192.168.1.100  10:48:50 up  2:29,  0 users,  load average: 0.000.000.00
    192.168.1.127 is not exists
    192.168.1.210  22:48:51 up  2:28,  0 users,  load average: 0.000.000.00
    192.168.1.51  10:48:52 up  2:28,  0 users,  load average: 0.000.000.00
    192.168.1.55  10:50:26 up  2:29,  0 users,  load average: 0.000.000.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.751.460.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模块

  • 相关阅读:
    java 之 对象与垃圾回收
    Java 之 内部类
    java 接口(interface)
    抽象类
    Java之fianl修饰符
    类的继承
    上传图片
    一般处理程序 给图片添加文字水印
    socket
    初识linq,lambda表达式
  • 原文地址:https://www.cnblogs.com/patrick0715/p/5885663.html
Copyright © 2011-2022 走看看