Advertisement
Guest User

netmiko

a guest
Nov 2nd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. import pdb # Set breakpoints in script for debugging with this line: pdb.set_trace()
  2. import inspect
  3. import subprocess
  4. import re
  5. import socket
  6. import sys
  7. import time
  8. import os
  9. import paramiko as pm
  10. import lib.functions as fn
  11. import lib.user_functions as ufn
  12. import lib.ssh_functions as sfn
  13. from netmiko import ConnectHandler
  14. import os.path
  15. from datetime import datetime
  16. from getpass import getpass
  17.  
  18. username = raw_input('Enter your SSH username: ')
  19. password = getpass()
  20.  
  21. with open('./devices_ip/devices_file') as f:
  22. devices_list = f.read().splitlines()
  23.  
  24. with open('./commands_folder/commands_ios_file') as f:
  25. commands_list_ios = f.read().splitlines()
  26.  
  27. with open('./commands_folder/commands_hp_file') as f:
  28. commands_list_hp = f.read().splitlines()
  29.  
  30. with open('./commands_folder/commands_icx_file') as f:
  31. commands_list_icx = f.read().splitlines()
  32.  
  33. #dictionary of R2 parameters, secret is an optional requirement for ConnectHandler
  34. #secret is actually for enable secret if local user account has no privilege 15
  35.  
  36. for devices in devices_list:
  37. ip_address_of_device = devices
  38. Cisco_device = {
  39. 'device_type': 'cisco_ios',
  40. 'ip': ip_address_of_device,
  41. 'username': username,
  42. 'password': password
  43. }
  44. ICX_device = {
  45. 'device_type': 'ruckus_fastiron',
  46. 'ip': ip_address_of_device,
  47. 'username': username,
  48. 'password': password
  49. }
  50. hp_device = {
  51. 'device_type': 'hp_procurve',
  52. 'ip': ip_address_of_device,
  53. 'username': username,
  54. 'password': password
  55. }
  56.  
  57. #this is to prepare for connection and go directly to privilege exec mode for each models type base on the menu entry
  58.  
  59. def r2_connect():
  60.  
  61. try:
  62. r2 = ConnectHandler(**Cisco_device)
  63. r2.enable()
  64. return r2
  65. except IOError:
  66. print("There is a problem connecting to the switches, please check if your switch is online")
  67.  
  68. def r3_connect():
  69.  
  70. try:
  71. r3 = ConnectHandler(**ICX_device)
  72. r3.enable()
  73. return r3
  74. except IOError:
  75. print("There is a problem connecting to the switches, please check if your switch is online")
  76.  
  77. def r5_connect():
  78.  
  79. try:
  80. r5 = ConnectHandler(**hp_device)
  81. r5.enable()
  82. return r5
  83. except IOError:
  84. print("There is a problem connecting to the switches, please check if your switch is online")
  85.  
  86. # Loop menu in case invalid option is selected
  87. while True:
  88. # Loop menu in case incorrect option is selected, and user declines confirming menu option
  89. while True:
  90. print "\n"
  91. print "------------------ Nettools v2 ------------------ \n"
  92. print "1) SSH Switches & Push commands"
  93. print "2) Setup VLAN (Work in progress)"
  94. print "9) Exit menu\n"
  95. menuChoice = raw_input("Choose a menu option: ")
  96.  
  97. # If menu choice is anything but 9 (exit menu), confirm with user
  98. if fn.isEmpty(menuChoice):
  99. print "\nInvalid menu option selected\n"
  100. mainMenuChoice = ''
  101. continue
  102.  
  103. if menuChoice != '9':
  104. menuConfirm = raw_input("\nConfirm menu choice of %s (y/n): " % (menuChoice))
  105.  
  106. # Check to see if entered value is 'y' or 'n' only
  107. if menuConfirm != 'y' and menuConfirm != 'n':
  108. print "\nPlease enter \"y\" or \"n\" only."
  109. continue
  110. elif menuConfirm == 'n':
  111. continue
  112. elif menuConfirm == 'y':
  113. break
  114. # Mainly used for debugging. This should never trigger
  115. else:
  116. fn.debugErrorOut('101')
  117. # Menu option is 9, so break initial menu selection loop
  118. else:
  119. break
  120.  
  121. # Execute commands based off menu choice
  122. if menuChoice == '9':
  123. print "\nExiting script\n"
  124. sys.exit()
  125.  
  126. if menuChoice == '1':
  127. while True:
  128. model = raw_input("\nEnter the models type of your switch: " )
  129. if model != 'HP' and model != 'Cisco' and model != 'ICX':
  130. print "\nPlease enter HP , Cisco or ICX only. \n"
  131. continue
  132. elif model == 'HP':
  133. r5 = r5_connect()
  134. output = r5.send_config_set(commands_list_hp)
  135. r5.disconnect()
  136. print output
  137. print "\n Connection has been established and commands has been pushed successfully for your HP Devices...\n"
  138. break
  139. elif model == 'Cisco':
  140. r2 = r2_connect()
  141. r2.send_command('terminal length 0')
  142. output = r2.send_config_set(commands_list_ios)
  143. r2.disconnect()
  144. print output
  145. print "\n Connection has been established and commands has been pushed successfully for your Cisco Devices...\n"
  146. elif model == 'ICX':
  147. r3 = r3_connect()
  148. r3.send_command('terminal length 0')
  149. output = r3.send_config_set(commands_list_icx)
  150. r3.disconnect()
  151. print output
  152. print "\n Connection has been established and commands has been pushed successfully for your ICX Devices...\n"
  153. break
  154. if menuChoice == '2':
  155. while True:
  156. model = raw_input("\nEnter the models type of your switch: " )
  157. if model != 'HP' and model != 'Cisco' and model != 'ICX':
  158. print "\nPlease enter HP , Cisco or ICX only. \n"
  159. continue
  160. elif model == 'Cisco':
  161. vlan_lookup = raw_input("\n Enter the VLAN to base the config on i.e 1000: " )
  162. vlan = vlan_lookup
  163. #vlans = re.findall(r'vlan\S\d+(.*)') #Probably a better way to do this
  164. #if vlan not in vlans:
  165. # r2 = r2_connect()
  166. # r2.send_config_set(
  167. # 'int vlan {vlan}',
  168. # )
  169. vlan_creation = raw_input("\n Enter the VLAN you want to add on the switch i.e 1017: ")
  170. vlan_name = raw_input("\n Enter the name of the VLAN you want to create, no space i.e MARSHA_Associate: ")
  171. command = [
  172. 'do show run vlan ' + vlan_lookup,
  173. 'vlan ' + vlan_creation,
  174. 'name ' + vlan_name
  175. ]
  176. r2 = r2_connect()
  177. #vlan_config = r2.send_config_set('show run vlan')
  178. output = r2.send_config_set(command)
  179. print output
  180. r2.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement