zoukankan      html  css  js  c++  java
  • paramiko 模块 ---- python2.7

    模拟远程执行命令: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import paramiko
     
    #设置日志记录
    paramiko.util.log_to_file('/tmp/test')
     
    #建立连接
    ssh=paramiko.SSHClient()
     
    #缺失host_knows时的处理方法
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     
    #连接远程客户机器
    ssh.connect('10.1.6.190',port=22,username='root',password='password',compress=True)
     
    #获取远程命令执行结果
    stdin, stdout, stderr = ssh.exec_command('hostname;uptime')
    print stdout.read()# 其中其他各类的提示在stderr,stdin中(如错误提示)
     
    #输出执行结果
    ssh.close()
    模拟远程文件传输:
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    import paramiko
     
    #建立一个加密的管道
    scp=paramiko.Transport(('10.1.6.190',22))
     
    #建立连接
    scp.connect(username='root',password='password')
     
    #建立一个sftp客户端对象,通过ssh transport操作远程文件
    sftp=paramiko.SFTPClient.from_transport(scp)
     
    #Copy a remote file (remotepath) from the SFTP server to the local host
    sftp.get('/root/debian7','/tmp/debian7')
     
    #Copy a local file (localpath) to the SFTP server as remotepath
    sftp.put('/root/crash-6.1.6.tar.gz','/tmp/crash-6.1.6.tar.gz')
     
    scp.close()
     
     
     
    未完待续……
     
     
     
     
     
  • 相关阅读:
    Github基础(一)
    python3中编码和解码
    sql面试题(一)
    sql查询(三)--having子句求众数、中位数
    sql查询
    numpy中ndarray数据结构简介
    qt信号和槽
    logtag查看log
    AT45DB161D 概述
    C中的auto、static、register、extern、const和volitate
  • 原文地址:https://www.cnblogs.com/wipy/p/4094308.html
Copyright © 2011-2022 走看看