python 多線程 paramiko實現批量命令輸入輸出


 

遠程批量執行命令  實現多線程執行 速度快 實現多並發登錄 

#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko
import threading
 
def ssh2(ip,username,passwd,cmd):
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,22,username,passwd,timeout=5)
        for m in cmd:
            stdin, stdout, stderr = ssh.exec_command(m)
#           stdin.write("Y")   #簡單交互,輸入 ‘Y’
            out = stdout.readlines()
            #屏幕輸出
            for o in out:
                print o,
        print '%s\tOK\n'%(ip)
        ssh.close()
    except :
        print '%s\tError\n'%(ip)
 
 
if __name__=='__main__':
    cmd = ['cal','echo hello!']#你要執行的命令列表
    username = "root"  #用戶名
    passwd = "123123"    #密碼
    threads = []   #多線程
    print "Begin......"
#    for i in range(1,254):
    ip = '192.168.1.19'
    a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
    a.start()

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM