bwks / netconnect

Connect to devices via telnet and ssh
GNU General Public License v3.0
7 stars 0 forks source link

Async helper method #23

Open bwks opened 6 years ago

bwks commented 6 years ago

Having some async helpers for dealing with large number of hosts would be a nice addition.

bwks commented 6 years ago

edited from: https://stackoverflow.com/a/15144765/2520425

from netconnect.cisco import CiscoDriver
from multiprocessing.pool import ThreadPool as Pool

ios01 = '192.168.121.153'
ios02 = '192.168.121.218'

items = [ios01, ios02]

pool_size = 5

def worker(item):
    commands = ['show run | i hostname']
    with CiscoDriver(item, 'vagrant', 'vagrant', disable_host_key_checking=True) as dev:
        result = dev.send_commands(commands)
        for i in result:
            print(i)

pool = Pool(pool_size)

for item in items:
    pool.apply_async(worker, (item,))

pool.close()
pool.join()