Advertisement
Guest User

pYTHON1

a guest
Sep 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. def execute(device, cmd):
  2. from netmiko import ConnectHandler
  3.  
  4. net_connect = ConnectHandler(**device) # **kwargs, un dictionar de lungime varaibila pasat ca arg
  5. net_connect.enable()
  6.  
  7. output = net_connect.send_command(cmd)
  8. print(output)
  9.  
  10.  
  11. if __name__ == '__main__':
  12. import threading
  13. import time
  14.  
  15. print('MAIN ' * 30)
  16. cisco_device1 = {'device_type': 'cisco_ios', 'ip': '192.168.122.100', 'username': 'u1', 'password': 'cisco',
  17. 'port': 22,
  18. 'secret': 'cisco', 'verbose': True}
  19. cisco_device2 = {'device_type': 'cisco_ios', 'ip': '192.168.122.200', 'username': 'u1', 'password': 'cisco',
  20. 'port': 22,
  21. 'secret': 'cisco', 'verbose': True}
  22.  
  23. devices = []
  24. devices.append(cisco_device1)
  25. devices.append(cisco_device2)
  26. print(devices)
  27.  
  28. start = time.time()
  29. execute(cisco_device1, 'sh run')
  30. execute(cisco_device2, 'sh run')
  31. end = time.time()
  32. normal_time = end - start
  33. threads = []
  34.  
  35. start = time.time()
  36. for device in devices:
  37. th = threading.Thread(target=execute,
  38. args=(
  39. device, 'sh run')) # virgula e pentru a-l pacacli ca e tuplu t = (s,) type(t) = touple
  40. threads.append(th)
  41.  
  42. for th in threads:
  43. th.start()
  44.  
  45. for th in threads:
  46. th.join()
  47.  
  48. end = time.time()
  49. thread_time = end - start
  50.  
  51. print(f'Normal vs thread: {normal_time} vs. {thread_time}')
  52. print('MAIN ' * 30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement