multithreading for paramiko

假如想象 提交于 2019-12-06 14:49:19
from multiprocessing.pool import ThreadPool
import paramiko
hosts = []
hostnames = []


def capture_travelinfo(host):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print('Connecting to ' + host)
    ssh.connect(host, username='root', key_filename='...')
    print('Connected to ' + host)
    stdin, stdout, stderr = ssh.exec_command('curl localhost:4000')
    stdin.close()
    ssh.close()


def main():
    ips = open('IPs.txt')
    pool = ThreadPool(5)
    for ip in ips:
        fields = ip.strip().split()
        UNIT = [fields[0], fields[1]]
        hosts.append(UNIT)
    for ip in hosts:
        hostnames.append(ip[1])
    results = pool.map(capture_travelinfo, hostnames)
    pool.close()
    pool.join()
    print(results)

As your code is really bad, I am running out of patience to carefully debug it. This version will almost work, just have a check later by yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!