Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. [
  2. {
  3. "sourceDevice": "APPSERVER-01",
  4. "duplex": "Auto",
  5. "Network": "10.1.1 /24",
  6. "vlan": "vlan 555",
  7. "NetworkSpeed": "Auto",
  8. "switchSpeed": "Auto",
  9. "destDevice": "NEXUS-5548-01",
  10. "blade": "10",
  11. "port": "10",
  12. "Os": "NX-OS",
  13. "sourceLocation": "SOME.RACK.100.01",
  14. "DestLocation": "SOME.RACK.102.01",
  15. "DestModel": "2248TP",
  16. "EquipmentConnectionID": "123456",
  17. "EquipType": "MGMT",
  18. "NetConfig": "g",
  19. "fexPrefix": "",
  20. "switchParentID": "21043",
  21. "switchParent": "DTC_LABS_CORE_7010_1",
  22. "Notes": "",
  23. "PortScriptFailed": "",
  24. "PortScriptDate": ""
  25. },
  26. {
  27. "sourceDevice": "APPSERVER-02",
  28. "duplex": "Auto",
  29. "Network": "10.1.1 /24",
  30. "vlan": "vlan 666",
  31. "NetworkSpeed": "Auto",
  32. "switchSpeed": "Auto",
  33. "destDevice": "NEXUS-5548-03",
  34. "blade": "10",
  35. "port": "10",
  36. "Os": "NX-OS",
  37. "sourceLocation": "SOME.RACK.100.02",
  38. "DestLocation": "SOME.RACK.102.02",
  39. "DestModel": "2248TP",
  40. "EquipmentConnectionID": "123456",
  41. "EquipType": "MGMT",
  42. "NetConfig": "g",
  43. "fexPrefix": "",
  44. "switchParentID": "21043",
  45. "switchParent": "DTC_LABS_CORE_7010_1",
  46. "Notes": "",
  47. "PortScriptFailed": "",
  48. "PortScriptDate": ""
  49. },
  50.  
  51. {
  52. "sourceDevice": "APPSERVER-03",
  53. "duplex": "Auto",
  54. "Network": "10.2.1.0/24",
  55. "vlan": "vlan 777",
  56. "NetworkSpeed": "Auto",
  57. "switchSpeed": "Auto",
  58. "destDevice": "DTC_LABS_CORE_7010_2",
  59. "blade": "10",
  60. "port": "10",
  61. "Os": "NX-OS",
  62. "sourceLocation": "SOME.RACK.117.14",
  63. "DestLocation": "SOME.RACK.118.40",
  64. "DestModel": "7018",
  65. "EquipmentConnectionID": "169914",
  66. "EquipType": "BI NIC",
  67. "NetConfig": "g",
  68. "fexPrefix": "",
  69. "switchParentID": "",
  70. "switchParent": "",
  71. "Notes": "HLPTICKET - 12345",
  72. "PortScriptFailed": "",
  73. "PortScriptDate": ""
  74. },
  75. {
  76. "sourceDevice": "APPSERVER-04",
  77. "duplex": "Auto",
  78. "Network": "10.2.1.0/24",
  79. "vlan": "vlan 777",
  80. "NetworkSpeed": "Auto",
  81. "switchSpeed": "Auto",
  82. "destDevice": "DTC_LABS_CORE_7010_2",
  83. "blade": "10",
  84. "port": "11",
  85. "Os": "NX-OS",
  86. "sourceLocation": "SOME.RACK.117.15",
  87. "DestLocation": "SOME.RACK.118.41",
  88. "DestModel": "7018",
  89. "EquipmentConnectionID": "169915",
  90. "EquipType": "BI NIC",
  91. "NetConfig": "g",
  92. "fexPrefix": "",
  93. "switchParentID": "",
  94. "switchParent": "",
  95. "Notes": "HLPTICKET - 1234567",
  96. "PortScriptFailed": "",
  97. "PortScriptDate": ""
  98. }
  99. ]
  100.  
  101. Switch 1: DTC_LABS_CORE_7010_1
  102. Port 1: int e10/10
  103.  
  104. Switch 2: DTC_LABS_CORE_7010_1
  105. Port 1: int e10/11
  106.  
  107. Switch 3: DTC_LABS_CORE_7010_2
  108. Port 1: int e10/10
  109.  
  110. Switch 4: DTC_LABS_CORE_7010_2
  111. Port 1: int e10/10
  112.  
  113. Switch 1: DTC_LABS_CORE_7010_1
  114. Port 1: int e10/10
  115. Port 2: int e10/11
  116.  
  117. Switch 2: DTC_LABS_CORE_7010_2
  118. Port 1: int e10/10
  119. Port 2: int e10/11
  120.  
  121. from __future__ import print_function
  122. import sys
  123. import json
  124. import time
  125. import os
  126. import re
  127. # import getpass
  128. from ciscoconfparse import CiscoConfParse
  129. from netmiko import ConnectHandler
  130. try:
  131. # For Python 3.0 and later
  132. from urllib.request import urlopen
  133. except ImportError:
  134. # Fall back to Python 2's urllib2
  135. from urllib2 import urlopen
  136.  
  137. # Grab the password from the user
  138. # password = getpass.getpass('Password:')
  139.  
  140. # This script is using test data rather than pulling from server
  141. # with open('proper-json-data.txt') as json_file:
  142. # data = json.load(json_file)
  143.  
  144. def get_jsonparsed_data(url):
  145. response = urlopen(url)
  146. data = response.read()
  147. return json.loads(data)
  148.  
  149.  
  150. url = ("http://www.somesite.com/api/ports/getPortActivations?building=dv")
  151. api_output = get_jsonparsed_data(url)
  152.  
  153. data = api_output["portActivations"]
  154.  
  155. # Define file to save output to
  156. logmyscript = open("pa4-config-changes-" + time.strftime('%b%d%Y') + ".txt", "w")
  157.  
  158. print ("*****" + time.strftime("%I:%M%p %Z on %b %d, %Y") + "*****")
  159. logmyscript.write("*****" + time.strftime("%I:%M%p %Z on %b %d, %Y") + " *****n")
  160.  
  161. # Creat dict mapping hostnames to IP address for hosts
  162. hosts = {}
  163. with open(r'netops-hosts.csv', 'r') as f:
  164. for line in f:
  165. hosts_line = line.strip('n').split(',')
  166. hosts[hosts_line[0]] = hosts_line[1]
  167.  
  168. # Define port activation list
  169. pa = []
  170.  
  171. for device in data:
  172. # if device.get('switchParent') is not None:
  173. if device['switchParent']:
  174. devicename = device['switchParent']
  175. else:
  176. devicename = device['destDevice']
  177.  
  178. if device['fexPrefix']:
  179. interface_prefix = device['fexPrefix']
  180. else:
  181. interface_prefix = device['blade']
  182.  
  183. interface = '{0}/{1}'.format(interface_prefix, device['port'])
  184.  
  185. description = '{0} - {1}'.format(device['sourceDevice'], device['sourceLocation'])
  186.  
  187. speed = device['switchSpeed']
  188. duplex = device['duplex']
  189. vlan = device['vlan']
  190.  
  191. pa.append('Modifying Device: {0}'.format(devicename))
  192. pa.append('interface Ethernet {0}'.format(interface))
  193. pa.append('no switchport mode access')
  194. pa.append('no switchport mode trunk')
  195. pa.append('description {0}'.format(description))
  196. if speed.lower() is 'auto':
  197. pa.append('no duplex')
  198. else:
  199. pa.append('duplex {0}'.format(duplex.lower()))
  200. pa.append('switchport')
  201. if vlan.lower() is 'trunk':
  202. pa.append('switchport mode trunk')
  203. pa.append('spanning-tree port type edge trunk')
  204. else:
  205. pa.append('switchport mode access')
  206. pa.append('switchport access {0}'.format(vlan))
  207. pa.append('spanning-tree port type edge')
  208. pa.append('no shut')
  209.  
  210. # get the address or return None if the hostname does not exsist
  211. deviceip = hosts.get(devicename)
  212. print ("Connecting to " + devicename + " at IP Address: " + deviceip)
  213. ssh_connection = ConnectHandler(device_type="cisco_nxos", ip=deviceip, username="myuser", password="mypass")
  214. runconf = ssh_connection.send_command('show running-config', delay_factor=2)
  215.  
  216. # convert object to a string for parsing
  217. conf_string = str(runconf).splitlines()
  218.  
  219. cisco_conf = CiscoConfParse(conf_string, factory=False)
  220. # Extract hostname and version from config and display
  221. device_hostname = cisco_conf.find_objects(r"^hostname")[0].text[9:]
  222. device_version = cisco_conf.find_objects(r"^version")[0].text[-4:]
  223. print ("[*] Config Parsed From: %s" % (device_hostname))
  224.  
  225. obj_list = cisco_conf.find_objects(r'Hostname')
  226. hostn = obj_list[0].hostname
  227. print ("Validate devices configured hostname: " + hostn)
  228.  
  229. config_list = []
  230. config_list.append('interface Ethernet {0}'.format(interface))
  231. config_list.append('no switchport mode access')
  232. config_list.append('no switchport mode trunk')
  233. config_list.append('description {0}'.format(description))
  234. if speed.lower() is 'auto':
  235. config_list.append('no duplex')
  236. else:
  237. config_list.append('duplex {0}'.format(duplex.lower()))
  238. config_list.append('switchport')
  239. if vlan.lower() is 'trunk':
  240. config_list.append('switchport mode trunk')
  241. config_list.append('spanning-tree port type edge trunk')
  242. else:
  243. config_list.append('switchport mode access')
  244. config_list.append('switchport access {0}'.format(vlan))
  245. config_list.append('spanning-tree port type edge')
  246. config_list.append('no shut')
  247. ssh_connection.enable()
  248. result = ssh_connection.find_prompt() + "n"
  249. ssh_connection.config_mode()
  250. ssh_connection.send_config_set(config_list)
  251.  
  252. # save recent changes to startup-config
  253. output = ssh_connection.send_command("copy run start")
  254. print (output)
  255. print ("Successfully configured " + hostn)
  256. print ("Disconnecting and moving on to next device...")
  257. # Disconnect from the router
  258. ssh_connection.disconnect()
  259.  
  260.  
  261. # Print changes to be made to screen and log file
  262. print('n'.join(pa), file = logmyscript)
  263. # sys.stdout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement