• python-paramiko


    安装paramiko

    [root@localhost ~]# pip3 install paramiko -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

    Collecting paramiko

    Downloading http://pypi.doubanio.com/packages/06/1e/1e08baaaf6c3d3df1459fd85f0e7d2d6aa916f33958f151ee1ecc9800971/paramiko-2.7.1-py2.py3-none-any.whl (206kB)

    100% |████████████████████████████████| 215kB 3.2MB/s

    Collecting cryptography>=2.5 (from paramiko)

    Downloading http://pypi.doubanio.com/packages/58/95/f1282ca55649b60afcf617e1e2ca384a2a3e7a5cf91f724cf83c8fbe76a1/cryptography-2.9.2-cp35-abi3-manylinux1_x86_64.whl (2.7MB)

    100% |████████████████████████████████| 2.7MB 20.9MB/s

    Collecting bcrypt>=3.1.3 (from paramiko)

    Downloading http://pypi.doubanio.com/packages/8b/1d/82826443777dd4a624e38a08957b975e75df859b381ae302cfd7a30783ed/bcrypt-3.1.7-cp34-abi3-manylinux1_x86_64.whl (56kB)

    100% |████████████████████████████████| 61kB 13.6MB/s

    Collecting pynacl>=1.0.1 (from paramiko)

    Downloading http://pypi.doubanio.com/packages/9d/57/2f5e6226a674b2bcb6db531e8b383079b678df5b10cdaa610d6cf20d77ba/PyNaCl-1.4.0-cp35-abi3-manylinux1_x86_64.whl (961kB)

    100% |████████████████████████████████| 962kB 22.6MB/s

    Collecting six>=1.4.1 (from cryptography>=2.5->paramiko)

    Downloading http://pypi.doubanio.com/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl

    Collecting cffi!=1.11.3,>=1.8 (from cryptography>=2.5->paramiko)

    Downloading http://pypi.doubanio.com/packages/f1/c7/72abda280893609e1ddfff90f8064568bd8bcb2c1770a9d5bb5edb2d1fea/cffi-1.14.0-cp36-cp36m-manylinux1_x86_64.whl (399kB)

    100% |████████████████████████████████| 399kB 53.7MB/s

    Collecting pycparser (from cffi!=1.11.3,>=1.8->cryptography>=2.5->paramiko)

    Downloading http://pypi.doubanio.com/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e71821/pycparser-2.20-py2.py3-none-any.whl (112kB)

    100% |████████████████████████████████| 112kB 8.1MB/s

    Installing collected packages: six, pycparser, cffi, cryptography, bcrypt, pynacl, paramiko

    Successfully installed bcrypt-3.1.7 cffi-1.14.0 cryptography-2.9.2 paramiko-2.7.1 pycparser-2.20 pynacl-1.4.0 six-1.15.0

    [root@localhost ~]#

    检查

    [root@localhost ~]# pip3 list

    DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

    bcrypt (3.1.7)

    cffi (1.14.0)

    cryptography (2.9.2)

    paramiko (2.7.1)

    pip (9.0.1)

    pycparser (2.20)

    PyNaCl (1.4.0)

    setuptools (28.8.0)

    six (1.15.0)

    API介绍

    paramiko提供了3个类,transport、channel、sshclient,sshclient可以看做是前两个类的高级封装

     

    示例

    transport

     

    import paramiko

    import time

    ip, port, username, password ='192.168.1.7',22,'root','123456'

    trans = paramiko.Transport((ip,port)) # ssh.connect集成了这2个命令

    trans.connect(username=username,password=password)

    chan = trans.open_session() #ssh.invoke_shell()集成了这3个命令

    chan.get_pty()

    chan.invoke_shell()

    chan.send('cd /tmp ')

    chan.send('ll ')

    time.sleep(1)

    out = chan.recv(1024)

    print(out.decode())

    chan.close()

    trans.close()

     

    sshclient.exec_command执行一条命令

     

    import paramiko

    ip, port, username, password,command='192.168.1.8',22,'root','123456','date'

    ssh = paramiko.SSHClient() #创建一个ssh对象

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #选择yes,接受key

    ssh.connect(hostname=ip, port=port, username=username, password=password,timeout=5) #连接服务器

    stdin, stdout, stderr = ssh.exec_command(command,get_pty=True) #执行一条命令

    print(stdout.read().decode('utf-8')) #获取执行命令的结果

    ssh.close() #关闭连接

    sshclient. invoke_shell发送多条命令

    import paramiko

    import time

    ip, port, username, password,command='192.168.1.8',22,'root','123456','date'

    ssh = paramiko.SSHClient() #创建一个ssh对象

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #选择yes,接受key

    ssh.connect(hostname=ip, port=port, username=username, password=password,timeout=5) #连接服务器

    chan = ssh.invoke_shell() #获取伪终端

    chan.send('cd /tmp ') #发送多个命令

    chan.send('ll ')

    time.sleep(1) #需要间隔时间,不然收不到输出

    out = chan.recv(1024) #接收命令结果

    print(out.decode('utf-8'))

    chan.close()

    ssh.close() #关闭连接

    ssh.open_sftp()建立sftp上传下载文件

    import paramiko,os

    ip, port, username, password,command='192.168.1.8',22,'root','123456','date'

    ssh = paramiko.SSHClient() #创建一个ssh对象

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #选择yes,接受key

    ssh.connect(hostname=ip, port=port, username=username, password=password,timeout=5) #连接服务器

    sftp = ssh.open_sftp()

    uploadlocalpath = os.path.join(os.getcwd(),'test.txt')

    uploadremotepath= '/tmp/test-1.txt'

    sftp.put(localpath=uploadlocalpath, remotepath=uploadremotepath)

    downloadlocalpath = os.path.join(os.getcwd(),'server-1.txt')

    downloadremotepath = '/tmp/server.txt'

    sftp.get(remotepath=downloadremotepath, localpath=downloadlocalpath)

    sftp.close()

    stdin, stdout, stderr = ssh.exec_command('ls /tmp',get_pty=True) #执行一条命令

    print(stdout.read().decode('utf-8')) #获取执行命令的结果

    ssh.close() #关闭连接

  • 相关阅读:
    备考C++有感
    使用GridView来获取xml文件数据
    SQL 事务及实例演示
    MySQL数据分析-(12)表操作补充:字段属性
    以字符串为例,谈谈Python到底要学到什么程度
    MySQL数据分析-(9)库操作补充:用户管理和权限管理
    Python流程控制和缩进
    MySQL数据分析-(11)表补充:数据类型
    MySQL数据分析-(8)SQL基础操作之库操作
    外键
  • 原文地址:https://www.cnblogs.com/jeancheng/p/13334146.html
Copyright © 2020-2023  润新知