Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import threading
- from Queue import Queue
- from getpass import getpass
- from netmiko import ConnectHandler
- from datetime import datetime
- # Define Variables
- # NOTE: raw input and getpass don't work in IDE
- username = raw_input('Username:')
- password = getpass()
- # Define Functions
- def ssh_session(row, output_q):
- output_dict = {}
- hostname = row['hostname']
- router = {'device_type': 'aruba_os', 'ip': hostname,
- 'username': username, 'password': password, 'verbose': False, }
- ssh_session = ConnectHandler(**router)
- # test command
- output = ssh_session.send_command("show ip interface brief | include 210 ")
- # Add data to the queue
- output_dict[hostname] = output
- output_q.put(output_dict)
- if __name__ == "__main__":
- # Set start time for timing
- startTime = datetime.now()
- output_q = Queue()
- # outfile = open('vlan2config.conf', 'w')
- with open('test.csv') as routerFile:
- routerDict = csv.DictReader(routerFile)
- for row in routerDict:
- # Start all threads
- print 'Starting ' + row['hostname']
- my_thread = threading.Thread(
- target=ssh_session, args=(row, output_q))
- my_thread.start()
- # Wait for all threads to complete
- main_thread = threading.currentThread()
- for some_thread in threading.enumerate():
- if some_thread != main_thread:
- some_thread.join()
- # Retrieve everything off the queue
- while not output_q.empty():
- my_dict = output_q.get()
- for k, val in my_dict.iteritems():
- print k
- print val
- # Write info to file
- # outfile.write(output)
- # outfile.close()
- # Print time to complete
- print datetime.now() - startTime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement