import paramiko import threadpool import time def ssh(hostname,port,username,password,cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname,port,username,password) stdin,stdout,stderr = ssh.exec_command(cmd) print(stdout.read().decode('utf-8')) if __name__ == '__main__': # t0 = time.time() # for i in range(100): # ssh('61.237.145.84',22,'ngse_admin','tianq@6675','df -h') # print('时间',time.time() - t0) t1 = time.time() pool = threadpool.ThreadPool(10) l = [(['1.1.1.1',22,'ngse_admin','tianq@6675','df -h'],None) for i in range(100)] requests = threadpool.makeRequests(ssh,l) [pool.putRequest(res) for res in requests] pool.wait() print('时间',time.time() - t1)
测试:
本机: 10个线程池 登录100台机子,用时7秒左右
串行登录,用时56秒以上