multithreading for paramiko

Question:

Context

If have a script that connects to servers and then curls the localhost for the information I need

Issue

My issue is that I have around 200 servers I need to grab information from, the method I use, takes about 15 minutes to finish, which isn’t bad but the stuff I’m wanting to do is more advanced and if I can nail multithreading I can achieve more.

Desired Outcome

I just want to have a threadpool of 5-10 workers so I can grab the information I need quicker.

Code

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)

Previous attempts

I’ve had a look around SO and found some stuff that relates but all the useful material doesn’t include thread pools and as a result I end up connecting to about 200 hosts at once which isn’t good.

Asked By: Danny Watson

||

Answers:

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)
Answered By: Sraw
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.