paramiko 的多线程
multithreading for paramiko
上下文
如果有一个连接到服务器的脚本,然后卷曲本地主机以获取我需要的信息
问题
我的问题是我有大约 200 台服务器需要从中获取信息,我使用的方法大约需要 15 分钟才能完成,这还不错,但我想做的事情更高级如果我能搞定多线程,我就能实现更多。
期望的结果
我只想拥有一个 5-10 人的线程池,这样我就可以更快地获取我需要的信息。
代码
from Queue import Queue
from multiprocessing.pool import ThreadPool
import threading, os, sys
import socket
from threading import Thread
#socket.setdefaulttimeout(5)
from time import sleep
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():
try:
file = open('IPs.txt')
threads = []
content = file
for x in content:
fields = x.strip().split()
UNIT = [fields[0], fields[1]]
hosts.append(UNIT)
for x in hosts:
host = x[1]
hostnames.append(x[1])
ip = x[0]
pool = ThreadPool(5)
results = pool.map(capture_travelinfo, hostnames)
pool.close()
pool.join()
print(results)
之前的尝试
我环顾四周,发现了一些相关的东西,但所有有用的东西 material 都不包括线程池,因此我最终一次连接到大约 200 台主机,这是不错
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)
由于你的代码实在是太烂了,我运行没耐心仔细调试了。这个版本差不多可以用了,待会儿自己检查一下。
上下文
如果有一个连接到服务器的脚本,然后卷曲本地主机以获取我需要的信息
问题
我的问题是我有大约 200 台服务器需要从中获取信息,我使用的方法大约需要 15 分钟才能完成,这还不错,但我想做的事情更高级如果我能搞定多线程,我就能实现更多。
期望的结果
我只想拥有一个 5-10 人的线程池,这样我就可以更快地获取我需要的信息。
代码
from Queue import Queue
from multiprocessing.pool import ThreadPool
import threading, os, sys
import socket
from threading import Thread
#socket.setdefaulttimeout(5)
from time import sleep
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():
try:
file = open('IPs.txt')
threads = []
content = file
for x in content:
fields = x.strip().split()
UNIT = [fields[0], fields[1]]
hosts.append(UNIT)
for x in hosts:
host = x[1]
hostnames.append(x[1])
ip = x[0]
pool = ThreadPool(5)
results = pool.map(capture_travelinfo, hostnames)
pool.close()
pool.join()
print(results)
之前的尝试
我环顾四周,发现了一些相关的东西,但所有有用的东西 material 都不包括线程池,因此我最终一次连接到大约 200 台主机,这是不错
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)
由于你的代码实在是太烂了,我运行没耐心仔细调试了。这个版本差不多可以用了,待会儿自己检查一下。