Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- {
- "sourceDevice": "APPSERVER-01",
- "duplex": "Auto",
- "Network": "10.1.1 /24",
- "vlan": "vlan 555",
- "NetworkSpeed": "Auto",
- "switchSpeed": "Auto",
- "destDevice": "NEXUS-5548-01",
- "blade": "10",
- "port": "10",
- "Os": "NX-OS",
- "sourceLocation": "SOME.RACK.100.01",
- "DestLocation": "SOME.RACK.102.01",
- "DestModel": "2248TP",
- "EquipmentConnectionID": "123456",
- "EquipType": "MGMT",
- "NetConfig": "g",
- "fexPrefix": "",
- "switchParentID": "21043",
- "switchParent": "DTC_LABS_CORE_7010_1",
- "Notes": "",
- "PortScriptFailed": "",
- "PortScriptDate": ""
- },
- {
- "sourceDevice": "APPSERVER-02",
- "duplex": "Auto",
- "Network": "10.1.1 /24",
- "vlan": "vlan 666",
- "NetworkSpeed": "Auto",
- "switchSpeed": "Auto",
- "destDevice": "NEXUS-5548-03",
- "blade": "10",
- "port": "10",
- "Os": "NX-OS",
- "sourceLocation": "SOME.RACK.100.02",
- "DestLocation": "SOME.RACK.102.02",
- "DestModel": "2248TP",
- "EquipmentConnectionID": "123456",
- "EquipType": "MGMT",
- "NetConfig": "g",
- "fexPrefix": "",
- "switchParentID": "21043",
- "switchParent": "DTC_LABS_CORE_7010_1",
- "Notes": "",
- "PortScriptFailed": "",
- "PortScriptDate": ""
- },
- {
- "sourceDevice": "APPSERVER-03",
- "duplex": "Auto",
- "Network": "10.2.1.0/24",
- "vlan": "vlan 777",
- "NetworkSpeed": "Auto",
- "switchSpeed": "Auto",
- "destDevice": "DTC_LABS_CORE_7010_2",
- "blade": "10",
- "port": "10",
- "Os": "NX-OS",
- "sourceLocation": "SOME.RACK.117.14",
- "DestLocation": "SOME.RACK.118.40",
- "DestModel": "7018",
- "EquipmentConnectionID": "169914",
- "EquipType": "BI NIC",
- "NetConfig": "g",
- "fexPrefix": "",
- "switchParentID": "",
- "switchParent": "",
- "Notes": "HLPTICKET - 12345",
- "PortScriptFailed": "",
- "PortScriptDate": ""
- },
- {
- "sourceDevice": "APPSERVER-04",
- "duplex": "Auto",
- "Network": "10.2.1.0/24",
- "vlan": "vlan 777",
- "NetworkSpeed": "Auto",
- "switchSpeed": "Auto",
- "destDevice": "DTC_LABS_CORE_7010_2",
- "blade": "10",
- "port": "11",
- "Os": "NX-OS",
- "sourceLocation": "SOME.RACK.117.15",
- "DestLocation": "SOME.RACK.118.41",
- "DestModel": "7018",
- "EquipmentConnectionID": "169915",
- "EquipType": "BI NIC",
- "NetConfig": "g",
- "fexPrefix": "",
- "switchParentID": "",
- "switchParent": "",
- "Notes": "HLPTICKET - 1234567",
- "PortScriptFailed": "",
- "PortScriptDate": ""
- }
- ]
- Switch 1: DTC_LABS_CORE_7010_1
- Port 1: int e10/10
- Switch 2: DTC_LABS_CORE_7010_1
- Port 1: int e10/11
- Switch 3: DTC_LABS_CORE_7010_2
- Port 1: int e10/10
- Switch 4: DTC_LABS_CORE_7010_2
- Port 1: int e10/10
- Switch 1: DTC_LABS_CORE_7010_1
- Port 1: int e10/10
- Port 2: int e10/11
- Switch 2: DTC_LABS_CORE_7010_2
- Port 1: int e10/10
- Port 2: int e10/11
- from __future__ import print_function
- import sys
- import json
- import time
- import os
- import re
- # import getpass
- from ciscoconfparse import CiscoConfParse
- from netmiko import ConnectHandler
- try:
- # For Python 3.0 and later
- from urllib.request import urlopen
- except ImportError:
- # Fall back to Python 2's urllib2
- from urllib2 import urlopen
- # Grab the password from the user
- # password = getpass.getpass('Password:')
- # This script is using test data rather than pulling from server
- # with open('proper-json-data.txt') as json_file:
- # data = json.load(json_file)
- def get_jsonparsed_data(url):
- response = urlopen(url)
- data = response.read()
- return json.loads(data)
- url = ("http://www.somesite.com/api/ports/getPortActivations?building=dv")
- api_output = get_jsonparsed_data(url)
- data = api_output["portActivations"]
- # Define file to save output to
- logmyscript = open("pa4-config-changes-" + time.strftime('%b%d%Y') + ".txt", "w")
- print ("*****" + time.strftime("%I:%M%p %Z on %b %d, %Y") + "*****")
- logmyscript.write("*****" + time.strftime("%I:%M%p %Z on %b %d, %Y") + " *****n")
- # Creat dict mapping hostnames to IP address for hosts
- hosts = {}
- with open(r'netops-hosts.csv', 'r') as f:
- for line in f:
- hosts_line = line.strip('n').split(',')
- hosts[hosts_line[0]] = hosts_line[1]
- # Define port activation list
- pa = []
- for device in data:
- # if device.get('switchParent') is not None:
- if device['switchParent']:
- devicename = device['switchParent']
- else:
- devicename = device['destDevice']
- if device['fexPrefix']:
- interface_prefix = device['fexPrefix']
- else:
- interface_prefix = device['blade']
- interface = '{0}/{1}'.format(interface_prefix, device['port'])
- description = '{0} - {1}'.format(device['sourceDevice'], device['sourceLocation'])
- speed = device['switchSpeed']
- duplex = device['duplex']
- vlan = device['vlan']
- pa.append('Modifying Device: {0}'.format(devicename))
- pa.append('interface Ethernet {0}'.format(interface))
- pa.append('no switchport mode access')
- pa.append('no switchport mode trunk')
- pa.append('description {0}'.format(description))
- if speed.lower() is 'auto':
- pa.append('no duplex')
- else:
- pa.append('duplex {0}'.format(duplex.lower()))
- pa.append('switchport')
- if vlan.lower() is 'trunk':
- pa.append('switchport mode trunk')
- pa.append('spanning-tree port type edge trunk')
- else:
- pa.append('switchport mode access')
- pa.append('switchport access {0}'.format(vlan))
- pa.append('spanning-tree port type edge')
- pa.append('no shut')
- # get the address or return None if the hostname does not exsist
- deviceip = hosts.get(devicename)
- print ("Connecting to " + devicename + " at IP Address: " + deviceip)
- ssh_connection = ConnectHandler(device_type="cisco_nxos", ip=deviceip, username="myuser", password="mypass")
- runconf = ssh_connection.send_command('show running-config', delay_factor=2)
- # convert object to a string for parsing
- conf_string = str(runconf).splitlines()
- cisco_conf = CiscoConfParse(conf_string, factory=False)
- # Extract hostname and version from config and display
- device_hostname = cisco_conf.find_objects(r"^hostname")[0].text[9:]
- device_version = cisco_conf.find_objects(r"^version")[0].text[-4:]
- print ("[*] Config Parsed From: %s" % (device_hostname))
- obj_list = cisco_conf.find_objects(r'Hostname')
- hostn = obj_list[0].hostname
- print ("Validate devices configured hostname: " + hostn)
- config_list = []
- config_list.append('interface Ethernet {0}'.format(interface))
- config_list.append('no switchport mode access')
- config_list.append('no switchport mode trunk')
- config_list.append('description {0}'.format(description))
- if speed.lower() is 'auto':
- config_list.append('no duplex')
- else:
- config_list.append('duplex {0}'.format(duplex.lower()))
- config_list.append('switchport')
- if vlan.lower() is 'trunk':
- config_list.append('switchport mode trunk')
- config_list.append('spanning-tree port type edge trunk')
- else:
- config_list.append('switchport mode access')
- config_list.append('switchport access {0}'.format(vlan))
- config_list.append('spanning-tree port type edge')
- config_list.append('no shut')
- ssh_connection.enable()
- result = ssh_connection.find_prompt() + "n"
- ssh_connection.config_mode()
- ssh_connection.send_config_set(config_list)
- # save recent changes to startup-config
- output = ssh_connection.send_command("copy run start")
- print (output)
- print ("Successfully configured " + hostn)
- print ("Disconnecting and moving on to next device...")
- # Disconnect from the router
- ssh_connection.disconnect()
- # Print changes to be made to screen and log file
- print('n'.join(pa), file = logmyscript)
- # sys.stdout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement