一些好用的库 paramiko

pip3 install -r requirements.txt

pip freeze  # 生成

paramiko

 基础使用

import paramiko

def testSSH():
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.188.200',22,'root','admin')   # if use sshkey  can ignor username and password
    stdin,stdout,stderr = ssh.exec_command('ifconfig')
    print(stdout.read().decode())
    ssh.close()

def testSFTP():
    trs = paramiko.Transport(('192.168.188.200',22))
    trs.connect(username='root',password='admin')
    sftp = paramiko.SFTPClient.from_transport(trs)
    sftp.put('./tests.py','/tmp/mustname.py')
    sftp.get('/tmp/fileremote','./fileremote')
    trs.close()

testSFTP()
View Code
原文地址:https://www.cnblogs.com/infaaf/p/8909552.html