Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. import csv
  2. import threading
  3. from Queue import Queue
  4. from getpass import getpass
  5. from netmiko import ConnectHandler
  6. from datetime import datetime
  7.  
  8. # Define Variables
  9. # NOTE: raw input and getpass don't work in IDE
  10. username = raw_input('Username:')
  11. password = getpass()
  12.  
  13. # Define Functions
  14.  
  15.  
  16. def ssh_session(row, output_q):
  17.  
  18.     output_dict = {}
  19.     hostname = row['hostname']
  20.  
  21.     router = {'device_type': 'aruba_os', 'ip': hostname,
  22.               'username': username, 'password': password, 'verbose': False, }
  23.     ssh_session = ConnectHandler(**router)
  24.  
  25.     # test command
  26.     output = ssh_session.send_command("show ip interface brief | include 210 ")
  27.  
  28.     # Add data to the queue
  29.     output_dict[hostname] = output
  30.     output_q.put(output_dict)
  31.  
  32. if __name__ == "__main__":
  33.  
  34.     # Set start time for timing
  35.     startTime = datetime.now()
  36.  
  37.     output_q = Queue()
  38.     # outfile = open('vlan2config.conf', 'w')
  39.  
  40.     with open('test.csv') as routerFile:
  41.         routerDict = csv.DictReader(routerFile)
  42.         for row in routerDict:
  43.  
  44.             # Start all threads
  45.             print 'Starting ' + row['hostname']
  46.             my_thread = threading.Thread(
  47.                 target=ssh_session, args=(row, output_q))
  48.             my_thread.start()
  49.  
  50.     # Wait for all threads to complete
  51.     main_thread = threading.currentThread()
  52.     for some_thread in threading.enumerate():
  53.         if some_thread != main_thread:
  54.             some_thread.join()
  55.  
  56.     # Retrieve everything off the queue
  57.     while not output_q.empty():
  58.         my_dict = output_q.get()
  59.         for k, val in my_dict.iteritems():
  60.             print k
  61.             print val
  62.  
  63.             # Write info to file
  64.             # outfile.write(output)
  65.  
  66.     # outfile.close()
  67.     # Print time to complete
  68.     print datetime.now() - startTime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement