Advertisement
Guest User

netmiko

a guest
Oct 29th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #config multiple devices switch with a commands_file
  2. #!/usr/bin/env python
  3.  
  4. from getpass import getpass
  5. from netmiko import ConnectHandler
  6. from netmiko.ssh_exception import NetMikoTimeoutException
  7. from netmiko.ssh_exception import SSHException
  8. from netmiko.ssh_exception import AuthenticationException
  9.  
  10. username = raw_input('Enter your SSH username: ')
  11. password = getpass()
  12.  
  13. #command file to be sent
  14.  
  15. with open('./commands_folder/commands_icx_file') as f:
  16. commands_list_hp = f.read().splitlines()
  17.  
  18. with open('./commands_folder/commands_hp_file') as f:
  19. commands_list_icx = f.read().splitlines()
  20.  
  21. with open('./commands_folder/commands_ios_file') as f:
  22. commands_list_ios = f.read().splitlines()
  23.  
  24. with open('./commands_folder/commands_file') as f:
  25. commands_to_send = f.read().splitlines()
  26.  
  27. #switch ip list to connect to
  28. with open('./devices_ip/devices_file') as f:
  29. devices_list = f.read().splitlines()
  30.  
  31. #library of devices
  32.  
  33. for devices in devices_list:
  34. print 'Connecting to device...'' ' + devices
  35. ip_address_of_device = devices
  36. ios_device = {
  37. 'device_type': 'cisco_ios',
  38. 'ip': ip_address_of_device,
  39. 'username': username,
  40. 'password': password
  41. }
  42.  
  43. try:
  44. net_connect = ConnectHandler(**ios_device)
  45. except (AuthenticationException):
  46. print 'Authentication failure: ' + ip_address_of_device
  47. continue
  48. except (NetMikoTimeoutException):
  49. print 'Timeout to device: ' + ip_address_of_device
  50. continue
  51. except (EOFError):
  52. print "End of file while attemtping device" + ip_address_of_device
  53. continue
  54. except (SSHException):
  55. print 'SSH Issue. Are you sure SSH is enable? ' + ip_address_of_device
  56. continue
  57. except Exception as unknow_error:
  58. print 'Some other error: ' + unknow_error
  59. continue
  60.  
  61. # Types of devices
  62. list_versions = ['ICX7150-',
  63. 'WS-C3',
  64. 'HP'
  65. ]
  66.  
  67. # Checking the software/type version
  68. for software_ver in list_versions:
  69. print 'Checking for ' + software_ver
  70. output_version = net_connect.send_command('show version')
  71. int_version = 0 # Reset integer value
  72. int_version = output_version.find(software_ver) # checking software version / models type
  73. if int_version > 0:
  74. print 'Software version found: ' + software_ver
  75. break
  76. else:
  77. print 'Did not find ' + software_ver
  78.  
  79. if software_ver == 'ICX7150-48' :
  80. print 'Running ' + software_ver + ' commands'
  81. output = net_connect.send_config_set(commands_list_icx)
  82. elif software_ver == 'WS-C3':
  83. print 'Running ' + software_ver + ' commands'
  84. output = net_connect.send_config_set(commands_list_ios)
  85. elif software_ver == 'HP':
  86. print 'Running ' + software_ver + ' commands'
  87. output = net_connect.send_config_set(commands_list_hp)
  88. print output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement