paramiko模块

#远程连接服务器并且执行命令
import paramiko import time import web def Print_creen(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='192.168.50.3',port=22,username='root',password='123456') stdin,stdout,stderr=ssh.exec_command('df -h') result= stdout.read() results = result.decode('utf8') print(results) ssh.close() return results Print_creen()

 paramiko sftp传输文件的方法

import paramiko
transport = paramiko.Transport(('192.168.50.3',22))
transport.connect(username='root',password='123456')
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put('C:/\/Users\/find-PC\Desktop\webpy-webpy-0.37.zip','/tmp/webpy-webpy-0.37.zip')
transport.close()

 paramiko批量执行命令,有点垃圾写的

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import paramiko
import sys
import time
import threading
def Para_batch(ip,cmd):
    username = 'root'
    password = '123456'
    port = 22
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(hostname=ip,username=username,port=port,password=password)
        stdin,stdout,stderr = ssh.exec_command(cmd)
        time.sleep(2)
        result = stdout.read().decode('utf-8')
        ssh.close()
        print(ip)
        print(result)
    except:
        print("%s is connect timeout!")
if __name__ == "__main__":
    cmd = sys.argv[1]
    for i in range(10):
        ip = "192.168.50.%s"%i
        a = threading.Thread(target=Para_batch,args=(ip,cmd))
        a.start()

  

posted on 2019-04-05 17:47  杨秀峰blog  阅读(132)  评论(0编辑  收藏  举报

导航