def import_asns(): if request.method == 'GET': devices = ['DC1-coresw', 'DC2-coresw', 'DC3-coresw'] aslist = [] for dev in devices: device = util.CiscoDeviceRO(host=dev) dev_connect = Netmiko(**device.__dict__) bgp_out = dev_connect.send_command("show ip bgp vrf all all") bgp_parsed = parse_output(platform="cisco_nxos", command="show ip bgp", data=bgp_out) dev_connect.disconnect() print ("Collected BGP table for " + dev) print ('<br />') bgpasns = [ sub['as_path'] for sub in bgp_parsed ] for asn in bgpasns: asnsplit = asn.split() for asnum in asnsplit: if asnum not in aslist and int(asnum) >=64512 and int(asnum) <=65535: aslist.append(asnum) as_map = map(int, aslist) as_sort = sorted(as_map) for sort in as_sort: asn_status = BGP.query.get(sort) if asn_status == None: new_asn = BGP(number=sort, desc='Imported from Core BGP Table') try: db.session.add(new_asn) db.session.commit() except: continue return redirect('/')
dev = (str(dev)[2:-2]) # print (dev) if dev not in devlist1: devlist1.append(dev) inv = [] print("duplicates filtered") """ Code below use try/except/finally blocks to be sure to close router connections and revert lists and also continue the loops in case the script has a problem. Leverages the Netmiko function, and RO automation user in the utils.py, and the n9kswitch01 router to ping/resolve. Please adapt to other routers or hosts as appropriate. """ print("-- Collecting Device IP Addresses -- ") try: n9kswitch01 = util.CiscoDeviceRO(host="switch01") net_connect = Netmiko(**cisctc01ipt01.__dict__) for dev in devlist1: try: print(dev) ping1 = net_connect.send_command('ping ' + dev) if "%" not in ping1: ip = ping1.splitlines()[1].split()[6][:-1] else: ip = () print("couldn't ping or couldn't resolve name!") if ip: invline = (dev + " ansible_host=" + ip) inv.append(invline) print("ok")
#open CSV file and generate list with open(sourcefile, 'r') as f: reader = csv.reader(f) devlist = list(reader) print (devlist) print ("csv file processed") #close out the file f.close() print ("-- Filtering Duplicates -- ") #filter out non-unique entries using a new list dedup_devices = [] header = ["Device", "Image Version", "Hardware Version"] dedup_devices.append(header) for dev in devlist: dev = (str(dev)[2:-2]) my_device = util.CiscoDeviceRO(host=dev) net_connect = Netmiko(**my_device.__dict__) #standin for parsed content raw_ver = net_connect.send_command("show version") if "NX-OS" in raw_ver: version = net_connect.send_command("show version | include bootflash://") version = version.splitlines()[-1].split()[-1] version = (str(version)[13:-4]) else: version = net_connect.send_command("show version | include bootdisk") version = version.splitlines()[-1].split()[-1] version = (str(version)[11:-5]) supervisor = net_connect.send_command("show mod | include Supervisor") if not supervisor: model = net_connect.send_command("show mod") model = model.splitlines()[2].split()[-3]
#filter out non-unique entries using a new list devlist1 = [] unreachables = [] for dev in devlist: dev = (str(dev)[2:-2]) if dev not in devlist1: devlist1.append(dev) inv = [] print("duplicates filtered<br />") print("---Analyzing Device Configurations---<br />") print("<br />") try: for dev in devlist1: try: router = util.CiscoDeviceRO(host=dev) net_connect = Netmiko(**router.__dict__) ver = net_connect.send_command('show version') hostname = net_connect.send_command('show hostname') try: if "NX-OS" in ver: print("<br />") print(hostname.strip().split()[0] + "<br />") print("verified as nxos device <br />") try: showcfs = net_connect.send_command('show run | i cfs') # print (showcfs) if "distribute" in showcfs: print( "<strong>***VULNERABLE CONFIGURATION FOUND***<br />"
border-collapse: collapse; } td, th { border: 1px solid black; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style>''') now = str(datetime.datetime.now()) print ("<b>Pre-Change Checkout for device: " + hostname + "</b><br /> Checkout time: " + now + "<br />") switch = util.CiscoDeviceRO(host=hostname) net_connect = Netmiko(**switch.__dict__) sw_ver = util.nxos_check(hostname) vrf_list = util.vrf_list(hostname) #print (vrf_list) if sw_ver == "nxos": for vrf in vrf_list: dstfile = (hostname + "." + vrf + ".pre_checkout.csv") arp_raw = net_connect.send_command("show ip arp vrf " + vrf) iplist = util.iplist(arp_raw) print ("ARP Entries for VRF " + vrf + ": " + str(len(iplist)) + "<table><caption>VRF: " + vrf + " : ARP Entry Ping status</caption><tr><th>IP Address</th><th>Ping Status</th>") with open(dstfile, 'w', newline='') as w: for ipentry in iplist: iping = net_connect.send_command("ping " + ipentry + " vrf " + vrf + " count 2") if "100.00% packet loss" not in iping: status = "Success"
@author: gnasses screenscraping app for rapid network checkout of a server/device Takes user input for hostname Uses Netmiko library to login to devices Ping/traceroute from core switches Determined last L3 switch hop and infers redundant pair uses ping/arp to determine access ports and displays selected statistics """ from netmiko import Netmiko import util try: print("Routine Server Network Checkout:") #define ELR and CTC EBR01 dc1coreswitch1 = util.CiscoDeviceRO(host="dc1coreswitch1") dc2coreswitch1 = util.CiscoDeviceRO(host="dc2coreswitch1") ping1 = () #while loop for error handling of invalid hostname, lenth arbitrarily set to 50 while len(ping1) <= 50: #user input of servername to be tested hostname = input("Enter the Server Name to be tested: ") #connect to EBRs using Netmiko net_connect1 = Netmiko(**dc1coreswitch1.__dict__) net_connect2 = Netmiko(**dc2coreswitch1.__dict__) #ping the entered hostname ping1 = net_connect1.send_command("ping " + hostname + " count 100") ping2 = net_connect2.send_command("ping " + hostname + " count 100") #print error message and restanr while loop or indicate testing in process if len(ping1) <= 50: print("Could not resolve servername. Please try again.")
def refresh(): devices = ['mls_starting_switch'] db.session.query(MTU).delete() db.session.commit() while len(devices) >= 1: device = devices[0] print("Device = " + device) try: switch = util.CiscoDeviceRO(host=device) net_connect = Netmiko(**switch.__dict__) cdp_raw = net_connect.send_command("show cdp neighbor detail") sw_ver = util.nxos_check(device) if sw_ver == 'nxos': cdp_parsed = parse_output(platform="cisco_nxos", command="show cdp neighbor detail", data=cdp_raw) else: cdp_parsed = parse_output(platform="cisco_ios", command="show cdp neighbor detail", data=cdp_raw) # print (cdp_parsed) for sub in cdp_parsed: if sw_ver == "nxos": destination_host = sub['dest_host'].split('.')[0].split( '-')[0].split('(')[0].lower() management_ip = sub['mgmt_ip'] local_port = sub['local_port'] else: destination_host = sub['destination_host'].split( '.')[0].split('-')[0].split('(')[0].lower() management_ip = sub['management_ip'] local_port = sub['local_port'] if "mls" in destination_host: try: mtu_raw = net_connect.send_command("show interface " + local_port + " | include MTU") m = re.search('MTU\s\d+', mtu_raw) mtu = m.group(0) except: mtu = "Error finding MTU" print("Entry: " + destination_host, management_ip, local_port, mtu) known_device = [] known_device = MTU.query.filter( MTU.device == destination_host).all() if not known_device: if destination_host not in devices: devices.append(destination_host) print("# of devices: " + str(len(devices))) new_mtu = MTU(device=device, destination_host=destination_host, local_port=local_port, management_ip=management_ip, mtu=mtu) try: db.session.add(new_mtu) db.session.commit() except: print("could not add cdp entry") except: print("Could not connect to this device") finally: try: net_connect.disconnect() except: print("no connection to disconnect") devices.remove(device) mtus = MTU.query.order_by(MTU.id).all() return render_template('mtu_index.html', mtus=mtus)
dev = (str(dev)[2:-2]) # print (dev) if dev not in devlist1: devlist1.append(dev) inv = [] print("duplicates filtered") """ Code below use try/except/finally blocks to be sure to close router connections and revert lists and also continue the loops in case the script has a problem. Leverages the Netmiko function, and RO automation user in the utils.py, and the n9kswitch1 router to ping/resolve. Please adapt to other routers or hosts as appropriate. """ print("-- Collecting Device IP Addresses -- ") try: n9kswitch1 = util.CiscoDeviceRO(host="cisctc01ipt01") net_connect = Netmiko(**cisctc01ipt01.__dict__) for dev in devlist1: try: print(dev) ping1 = net_connect.send_command('ping ' + dev) if "%" not in ping1: ip = ping1.splitlines()[1].split()[6][:-1] else: ip = () print("couldn't ping or couldn't resolve name!") if ip: invline = (dev + " ansible_host=" + ip) inv.append(invline) print("ok")
border-collapse: collapse; } td, th { border: 1px solid black; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style>''') now = str(datetime.datetime.now()) try: mlselr01ebr01 = util.CiscoDeviceRO(host="mlselr01ebr01") mlsctc01ebr01 = util.CiscoDeviceRO(host="mlsctc01ebr01") ping1 = () while len(ping1) <= 50: net_connect1 = Netmiko(**mlselr01ebr01.__dict__) net_connect2 = Netmiko(**mlsctc01ebr01.__dict__) ping1 = net_connect1.send_command("ping " + hostname + " count 100") ping2 = net_connect2.send_command("ping " + hostname + " count 100") if len(ping1) <= 50: print("Could not resolve servername. Please try again.") else: print('<br />') server_ip1 = ping1.splitlines()[2].split()[3][:-1] server_ip2 = ping2.splitlines()[2].split()[3][:-1] trace1 = net_connect1.send_command("traceroute " + server_ip1) access1 = util.acc_sw(trace1)
dev = (str(dev)[2:-2]) # print (dev) if dev not in devlist1: devlist1.append(dev) inv = [] print ("duplicates filtered") """ Code below use try/except/finally blocks to be sure to close router connections and revert lists and also continue the loops in case the script has a problem. Leverages the Netmiko function, and RO automation user in the utils.py, and the "ciscorouter" router to ping/resolve. Please adapt to other routers or hosts as appropriate. """ print("-- Collecting Device IP Addresses -- ") try: ciscorouter = util.CiscoDeviceRO(host="ciscorouter) net_connect = Netmiko(**ciscorouter.__dict__) for dev in devlist1: try: print (dev) ping1 = net_connect.send_command('ping ' + dev) if "%" not in ping1: ip = ping1.splitlines()[1].split()[6][:-1] else: ip = () print ("couldn't ping or couldn't resolve name!") if ip: invline = (dev + " ansible_host=" + ip) inv.append(invline) print ("ok")