def get_ospf_routers(): """Gets mac and arp tables. Concatinates into one""" process, router_id = None, None # Delete table data DbOps.delete_rows('ospfrouters_front_end', device) try: for line in send_command( 'show ip ospf border-routers | ex Codes|Internal|Base' ).splitlines(): if len(line.split()) == 0: continue elif line.split()[0] == 'OSPF': router_id = line.split()[4].strip(')').strip('(') process = line.split()[7].strip(')') elif len(line.split()) == 11: DbOps.update_ospf_router_table( device, process, router_id, line.split()[1], line.split()[0], line.split()[2].strip(']').strip('['), line.split()[4].strip(','), line.split()[5].strip(','), line.split()[6].strip(','), f'{line.split()[7]} {line.split()[8].strip(",")}', line.split()[10]) except AttributeError: pass
def get_hsrp_status(): """Gets mac and arp tables. Concatinates into one""" # Delete table data DbOps.delete_rows('hsrp_front_end', device) try: for interface in send_command( 'show standby brief | ex Interface').splitlines(): if len(interface.split()) == 0: continue else: try: DbOps.update_hsrp_table(device, interface.split()[0], interface.split()[1], interface.split()[2], interface.split()[3], interface.split()[4], interface.split()[5], interface.split()[6], interface.split()[7]) except IndexError: pass except AttributeError: pass
def parse_poch_interface(form, mode, device, password, username): """Parse, build, send info for trunk configuration""" # Convert interface number back to slashes from %2f if form.get("pochCustomForm"): status = send_config_cli(device, username, password, form.get('pochCustomForm')) DbOps.update_configurations(device, username, form.get('pochCustomForm'), 'Add Link to Portchannel') else: int_num = [ i for i in form.get("interface") if i not in string.ascii_letters ] int_type = [ i for i in form.get("interface") if i in string.ascii_letters ] config = build_porch_link(''.join(int_type) + ''.join(int_num), form.get('pochannel'), mode) status = send_config(device, username, password, config) DbOps.update_configurations(device, username, config, 'Add Link to Portchannel') return status
def get_qos_interfaces(): """Gets QOS configurations""" # Deletes table data DbOps.delete_rows('InterfaceQos_front_end', device) make_ints_lists = is_instance(get_stats()) for i in make_ints_lists: if i.get("diffserv-target-entry", {}).get("direction", {}): for index, stat in enumerate( i.get("diffserv-target-entry", {}).get("diffserv-target-classifier-statistics", {})): DbOps.update_qos_table( device, i['name'], i.get('diffserv-target-entry', {}).get('policy-name', {}), i.get('diffserv-target-entry', {}).get('direction', {}), stat.get('classifier-entry-name', {}), stat.get("classifier-entry-statistics", {}).get("classified-rate", {}), stat.get('classifier-entry-statistics', {}).get('classified-bytes', {}), stat.get('classifier-entry-statistics', {}).get('classified-pkts', {}), stat.get('queuing-statistics', {}).get('output-bytes', {}), stat.get('queuing-statistics', {}).get('output-pkts', {}), stat.get('queuing-statistics', {}).get('drop-pkts', {}), stat.get('queuing-statistics', {}).get('drop-bytes', {}), stat.get('queuing-statistics', {}).get('wred-stats', {}).get('early-drop-pkts', {}), stat.get('queuing-statistics', {}).get('wred-stats', {}).get('early-drop-bytes', {}))
def get_vlans(): """Get vlans""" # Deletes table data DbOps.delete_rows('vlans_front_end', device) try: for vlan in send_command('show vlan brief').splitlines(): if len(vlan.split()) == 0: continue elif vlan.split()[0] == '^': break elif vlan.split()[0] == 'VLAN': continue elif vlan.split()[0] == '----': continue # Get vlan ports if vlan.split()[0].rfind("/") != -1: vlan_ports = ' '.join(vlan.split()) else: vlan_ports = ' '.join(vlan.split()[3:]) # Compare vlan id (show vlan) to vlan priority. Use indexing since vlan prio is VLAN + 4 ints, 0000 for prio in send_command( 'show spanning-tree bridge priority').splitlines(): if len(prio.split()) == 0: continue elif vlan.split()[0] == prio.split()[0][5:]: DbOps.update_vlan_table(device, vlan.split()[0], prio.split()[1], vlan.split()[1], vlan.split()[2], vlan_ports) break elif vlan.split()[0] == prio.split()[0][6:]: DbOps.update_vlan_table(device, vlan.split()[0], prio.split()[1], vlan.split()[1], vlan.split()[2], vlan_ports) break elif vlan.split()[0] == prio.split()[0][7]: DbOps.update_vlan_table(device, vlan.split()[0], prio.split()[1], vlan.split()[1], vlan.split()[2], vlan_ports) break else: DbOps.update_vlan_table(device, vlan.split()[0], 'N/A', vlan.split()[1], vlan.split()[2], vlan_ports) break except AttributeError: pass
def get_mac_arp_table(): """Gets mac and arp tables. Concatinates into one""" mac_table = [] arp_table = [] # Delete table data DbOps.delete_rows('arpmac_front_end', device) try: for mac in send_command( 'show mac address-table | ex Vlan|All|Total|%|-').splitlines(): try: mac_table.append({ 'vlan': mac.split()[0], 'address': mac.split()[1], 'type': mac.split()[2], 'interface': mac.split()[3] }) except IndexError: continue for arp in send_command( 'show ip arp | ex Protocol|Total|%').splitlines(): try: arp_table.append({ 'protocol': arp.split()[0], 'ip': arp.split()[1], 'age': arp.split()[2], 'mac': arp.split()[3], 'interface': arp.split()[5] }) except IndexError: continue # Check to see if mac has an arp entry. If so, add k/v to existing dictionary if mac_table: for mac in mac_table: for entry in arp_table: if mac.get('address') == entry.get('mac'): mac['ip'] = entry.get('ip') mac['ip_int'] = entry.get('interface') break else: mac['ip'] = 'None' mac['ip_int'] = 'None' if mac_table: for i in mac_table: DbOps.update_mac_arp_table(device, i['vlan'], i['address'], i['type'], i['interface'], i.get('ip'), i.get('ip_int')) except AttributeError: pass
def get_cdp_neighbors(): """Gets mac and arp tables. Concatinates into one""" neighbors = [] name = None local_port = None remote_port = None try: for neighbor in send_command('show cdp neighbors').splitlines(): try: if not neighbor: continue elif neighbor.split()[0] == "": continue elif neighbor.split()[0] == 'Capability': continue elif neighbor.split()[0] == 'Device': continue if len(neighbor.split()) == 1: name = neighbor.split()[0] elif len(neighbor.split()) == 7: remote_port = neighbor.split()[5] + neighbor.split()[6] local_port = neighbor.split()[0] + neighbor.split()[1] elif len(neighbor.split()) == 8: remote_port = neighbor.split()[6] + neighbor.split()[7] local_port = neighbor.split()[0] + neighbor.split()[1] elif len(neighbor.split()) == 9: remote_port = neighbor.split()[7] + neighbor.split()[8] local_port = neighbor.split()[0] + neighbor.split()[1] except IndexError: continue if remote_port is not None: neighbors.append({ 'name': name, 'local-port': local_port, 'remote-port': remote_port }) name = None local_port = None remote_port = None # Delete table data DbOps.delete_rows('cdp_front_end', device) for i in neighbors: db_session = DbOps.update_cdp_table(device, i['name'], i['local-port'], i['remote-port']) except AttributeError: pass
def add_devices(): login_form = LoginForm(request.form) if 'login' in request.form: netconf_port = request.form['netconf'] ssh_port = request.form['ssh'] if not netconf_port: netconf_port = 830 if not ssh_port: ssh_port = 22 # Attempt to create connection objects. Must have both to get to homepage netconf_session = ConnectWith.create_netconf_connection( request.form['username'], request.form['password'], request.form['device'], netconf_port) netmiko_session = ConnectWith.creat_netmiko_connection( request.form['username'], request.form['password'], request.form['device'], ssh_port) # Using netmiko and ncclient for connections, verify that both pass. If one fails, return to login if netmiko_session == 'Authenitcation Error': flash("Authentication Failure") return redirect(url_for('base_blueprint.add_devices')) elif netmiko_session == 'ssh_exception' or netmiko_session == 'Connection Timeout': flash("Check Device Connectivity") return redirect(url_for('base_blueprint.add_devices')) if netconf_session == 'Authentication Error': flash("Authentication Failure") return redirect(url_for('base_blueprint.add_devices')) elif netconf_session == 'Connection Timeout' or netconf_session == 'Connectivity Issue': flash("Check Device Connectivity") return redirect(url_for('base_blueprint.add_devices')) else: serial_model = GetFacts.get_serial_model(netmiko_session) uptime_software = GetFacts.get_serial_model(netmiko_session) DbOps.update_device_facts(request.form['device'], serial_model[0], serial_model[1], uptime_software[0], uptime_software[1], request.form['username'], request.form['password'], ssh_port, netconf_port) return redirect(url_for('base_blueprint.route_default')) else: return render_template('accounts/new_inventory_login.html', form=login_form)
def parse_int_form(form, device, password, username): """Parse, build, send info for interface configuration""" ip, mask, status, descr, vrf, switchport = None, None, None, None, None, None if not form.get('newIntCustomForm'): if form.get('ip') and form.get('mask'): ip = form.get('ip') mask = form.get('mask') if form.get('status'): status = form.get('status') if form.get('description'): descr = form.get('description') if form.get('vrf'): vrf = form.get('vrf') if form.get('negotiation'): negotiation = form.get('negotiation') if form.get('vlan'): negotiation = form.get('negotiation') if form.get('switchport'): switchport = form.get('switchport') # Convert interface number back to slashes from %2f int_num = [ i for i in form.get("interface") if i not in string.ascii_letters ] int_type = [ i for i in form.get("interface") if i in string.ascii_letters ] # Validate interface perameters validation = ValidateConfig.validate_interface( ''.join(int_type) + ''.join(int_num), ip, mask, status, vrf, descr, QueryDbFor.query_interfaces(device)) config = build_interface(''.join(int_type) + ''.join(int_num), ip, mask, status, vrf, descr, switchport) # If validation passes which is 0, send config, update DB if validation == 0: status = send_config(device, username, password, config) DbOps.update_configurations(device, username, config, "Modify/Add Interface") else: status = send_config_cli(device, username, password, form.get('newIntCustomForm')) DbOps.update_configurations(device, username, form.get('newIntCustomForm'), "Modify/Add Interface") return status
def parse_add_vlan_form(form, device, password, username): """Parse, build, send info for vlan configuration""" if not form.get('vlanCustomForm'): config = build_vlan(form.get('vlanId'), form.get('vlanName'), form.get('action')) status = send_config_cli(device, username, password, config) DbOps.update_configurations(device, username, config, 'Vlan') else: status = send_config_cli(device, username, password, form.get('vlanCustomForm')) DbOps.update_configurations(device, username, form.get('vlanCustomForm'), 'Vlan') return status
def get_vrfs(): """Get device model""" # Delete table data DbOps.delete_rows('vrfs_front_end', device) try: for i in send_command('show vrf').splitlines(): try: if i.rfind('Name') == -1: db_session = DbOps.update_vrfs_table(device, i.split()[0]) except IndexError: pass except AttributeError: pass
def get_ospf_status(): """Gets OSPF neighbor statuses""" neighbor_status = collections.defaultdict(list) # Delete table data DbOps.delete_rows('ospf_front_end', device) try: if send_command('show ip ospf neighbor').splitlines(): for i in send_command('show ip ospf neighbor').splitlines(): try: neighbor = ipaddress.ip_address(i.split()[0]) neighbor_status[neighbor].append({ "NeighborID": i.split()[0], 'State': i.split()[2].strip("/"), 'Address': i.split()[5], 'Interface': i.split()[6] }) except (IndexError, ValueError): pass else: if send_command('show ip ospf').splitlines(): neighbor_status['neighbor'].append({ "NeighborID": 'No Established Neighbors', 'State': 'None', 'Address': 'None', 'Interface': 'None' }) else: neighbor_status = [] if neighbor_status: for k, v in neighbor_status.items(): for i in v: db_session = DbOps.update_ospf_table( device, i['NeighborID'], i['State'], i['Address'], i['Interface']) except AttributeError: pass
def get_ospf_processes(): """Get OSPF processes""" # Delete table data DbOps.delete_rows('ospfProcess_front_end', device) try: if send_command('show ip ospf | i Process').splitlines(): for process in send_command( 'show ip ospf | i Process').splitlines(): try: DbOps.update_ospf_process_table( device, process.split('"')[1].split()[1]) except IndexError: continue except AttributeError: pass
def get_dmvpn(): """Gets dmvpn peers, attributes, status, writes to DB""" interface, router_type = None, None # Delete table data DbOps.delete_rows('dmvpn_front_end', device) for line in send_command('show dmvpn | b Interface').splitlines(): if len(line.split()) == 0 or '-' in line or '#' in line: continue elif len(line.split()) == 6: DbOps.update_dmvpn_table(device, line.split()[1], line.split()[2], line.split()[3], line.split()[4], line.split()[5])
def get_dmvpn_info(): """Gets dmvpn peers, attributes, status, writes to DB""" interface = None # Delete table data DbOps.delete_rows('dmvpncount_front_end', device) for line in send_command('show dmvpn | i Interface|Type').splitlines(): if len(line.split()) == 0: continue elif len(line.split()) == 5: interface = line.split()[1].strip(',') get_dmvpn_interface(session, interface, device) elif len(line.split()) == 3: router_type = line.split(':')[1].split(',')[0] peer_count = line.split()[2].strip('Peers:').strip(',') DbOps.update_dmvpn_count(device, interface, router_type, peer_count)
def get_prefix_lists(): """View current prefix-list, match statemnt combinations.""" # Deletes table data DbOps.delete_rows('PrefixList_front_end', device) prefix_lists = get_prefix_config() try: for prefix_list in prefix_lists: lists = is_instance(prefix_list.get("seq", {})) for sequence in lists: DbOps.update_prefix_table(device, prefix_list.get("name"), sequence.get("no"), sequence.get("action"), sequence.get("ge", ''), sequence.get("le")) except AttributeError: pass
def get_dmvpn_interface(session, interface, device): """Get route-map names""" ip_add, tunnel_source, tunnel_mode, network_id, holdtime, profile, nhrp_shortcut, nhrp_red = None, None, None, None, \ None, None, None, None DbOps.delete_rows('dmvpninterfaces_front_end', device) try: for line in send_command( f'show run interface {interface} | ex Current|Building|!', session).splitlines(): if len(line.split()) == 0: continue elif '^' == line.split()[0]: break elif 'network-id' in line: network_id = line.split()[3] elif 'interface' in line: pass elif 'address' in line: ip_add = f'{line.split()[2]} {line.split()[3]}' elif 'source' in line: tunnel_source = line.split()[2] elif 'mode' in line: tunnel_mode = f'{line.split()[2]} {line.split()[3]}' elif 'protection' in line: profile = line.split()[4] DbOps.update_dmvpn_interfaces(device, interface, ip_add, tunnel_source, tunnel_mode, network_id, holdtime, profile, nhrp_shortcut, nhrp_red) elif 'holdtime' in line: holdtime = line.split()[3] # Check dmvpn phase commands if 'shortcut' in line: nhrp_shortcut = line.split()[2] if 'redirect' in line: nhrp_red = line.split()[2] except AttributeError: pass
def get_route_maps(): """Get route-map names""" map_name = None # Deletes table data DbOps.delete_rows('routeMaps_front_end', device) try: for line in send_command('show route-map | i route-map').splitlines(): if not list(enumerate(line.split(), 0)): continue elif line.split()[0] == '^': break elif line.split()[1] != map_name: DbOps.update_route_maps(device, line.split()[1]) map_name = line.split()[1] except AttributeError: pass
def get_arp(): """Get ARP table""" # Delete table data DbOps.delete_rows('arp_front_end', device) try: for i in send_command('show ip arp').splitlines(): try: if i.split()[0] != 'Protocol': DbOps.update_arp_table(device, i.split()[0], i.split()[1], i.split()[2], i.split()[3], i.split()[4], i.split()[5]) except IndexError: pass except AttributeError: pass
def get_port_channels(): """Get port-channels""" port_channels = [] # Deletes table data DbOps.delete_rows('PoChannels_front_end', device) for ints in trunk_types: print(ints) try: # If data structure is a dict. This funtion with convert to a list. Dictionaries occur when there is one of something returned make_list = is_in_list( get_config()[0]["native"]["interface"].get(ints)) print(make_list) for interface in make_list: print(interface) if interface is None: continue elif interface.get("channel-group", {}).get("number", {}): port_channels.append({ 'interface': ints + interface.get("name"), 'group': interface.get('channel-group').get('number'), 'mode': interface.get('channel-group').get('mode') }) except TypeError: pass # Iterate through trunk list and interface state. Comapre interface banes and get interface state. Write to database for interface in port_channels: for port in get_stats(): if interface.get('interface') == port.get('name'): DbOps.update_pochannel_table(device, interface['interface'], interface['group'], interface['mode'], port.get('admin-status'), port.get('oper-status'))
def get_span_root(): """Gets mac and arp tables. Concatinates into one""" # Delete table data DbOps.delete_rows('spanningtree_front_end', device) try: for vlan in send_command( 'show spanning-tree root | ex Vlan|-|Root').splitlines(): if len(vlan.split()) == 0: continue elif len(vlan.split()) == 7: DbOps.update_spann_tree_table(device, vlan.split()[0].strip('VLAN'), vlan.split()[1], vlan.split()[2], vlan.split()[3], '') elif len(vlan.split()) == 8: DbOps.update_spann_tree_table(device, vlan.split()[0].strip('VLAN'), vlan.split()[1], vlan.split()[2], vlan.split()[3], vlan.split()[7]) except AttributeError: pass
def parse_trunk_interface(form, device, password, username): """Parse, build, send info for trunk configuration""" if form.get("trunkCustomForm"): status = send_config_cli(device, username, password, form.get("trunkCustomForm")) DbOps.update_configurations(device, username, form.get("trunkCustomForm"), 'Modify Trunk') else: config = build_add_trunk_vlan(''.join(int_type) + ''.join(int_num), form.get('vlan'), form.get('action')) # Convert interface number back to slashes from %2f int_num = [ i for i in form.get("interface") if i not in string.ascii_letters ] int_type = [ i for i in form.get("interface") if i in string.ascii_letters ] status = send_config(device, username, password, config) DbOps.update_configurations(device, username, config, 'Modify Trunk') return status
def get_bgp_status(): """Gets BGF neighbor statuses""" local_as = ['Null'] # Delete table data DbOps.delete_rows('bgp_front_end', device) try: for i in send_command('show ip bgp summary').splitlines(): if i.rfind('local AS number') != -1: local_as = i.split()[-1:] try: ipaddress.ip_address(i.split()[0]) DbOps.update_bgp_table(device, i.split()[0], i.split()[2], i.split()[8], i.split()[9], local_as) except (IndexError, ValueError): pass except AttributeError: pass
def parse_access_int_form(form, device, password, username): """Parse, build, send info for access interface configuration""" vlan, descr, status, voice_vlan = None, None, None, None if not form.get('vlanCustomForm'): if form.get('status'): status = form.get('status') if form.get('description'): descr = form.get('description') if form.get('vlan'): vlan = form.get('vlan') if form.get('voiceVlan') != 'None': voice_vlan = form.get('voiceVlan') else: voice_vlan = None # Convert interface number back to slashes from %2f int_num = [ i for i in form.get("interface") if i not in string.ascii_letters ] int_type = [ i for i in form.get("interface") if i in string.ascii_letters ] # Build and send config, updated DB status = send_config(device, username, password, config) DbOps.update_configurations(device, username, config, 'Modify Access Interface') else: status = send_config_cli(device, username, password, form.get('vlanCustomForm')) DbOps.update_configurations(device, username, form.get('vlanCustomForm'), 'Modify Access Interface') return status
def import_csv_bulk(path, filename): """Parse data from csv file upload, write to database devicefacts_front_end table""" with open(path) as file: for row_id, row in enumerate(csv.reader(file)): if row_id != 0: try: netmiko_session = ConnectWith.creat_netmiko_connection( row[1], row[2], row[0], row[3]) serial_model = get_serial_model(netmiko_session) uptime_software = get_uptime_software(netmiko_session) update_facts = DbOps.update_device_facts( row[0], serial_model[0], serial_model[1], uptime_software[0], uptime_software[1], row[1], row[2], row[3], row[4]) except IndexError: pass
def get_access_ports(): """Get trunks""" # Deletes table data DbOps.delete_rows('accessInterfaces_front_end', device) try: for line in send_command( 'show interfaces status | ex Port').splitlines(): if len(line.split()) == 0: continue elif line.split()[0] == '^': break else: if len(line.split()) == 7: DbOps.update_access_interfaces_table( device, line.split()[0], line.split()[1], line.split()[2], line.split()[3], line.split()[4], line.split()[5]) elif len(line.split()) == 6: DbOps.update_access_interfaces_table( device, line.split()[0], 'N/A', line.split()[1], line.split()[2], line.split()[4], line.split()[5]) elif len(line.split()) == 5: DbOps.update_access_interfaces_table( device, line.split()[0], 'N/A', line.split()[1], line.split()[2], line.split()[4], 'N/A') except AttributeError: pass
def gather_facts(): """Gets all things routing, arp, interfaces, routing protocols""" global model serial_model = GetFacts.get_serial_model(netmiko_session) model = serial_model[1] uptime_software = GetFacts.get_uptime_software(netmiko_session) update_facts = DbOps.update_device_facts(device, serial_model[0], serial_model[1], uptime_software[0], uptime_software[1], username, password, ssh_port, netconf_port) if update_facts is not None: GetNeconf.start_polling(username, password, device, serial_model[1][:3], netconf_port, ssh_port) GetNetmiko.start_polling(username, password, device, serial_model[1][:3], ssh_port) return redirect(url_for('base_blueprint.index'))
def parse_routing_config(form, device, password, username): """Parse, build, and send new routing configuration""" # Proccess for data from input fields if request.form.get("process") is not None: config = BuildConfig.build_ospf(request.form.get("process"), request.form.get("neighbor"), request.form.get("wildcard"), request.form.get("area")) status = BuildConfig.send_config(device, username, password, config) DbOps.update_configurations(device, username, config, 'Ospf') elif request.form.get("remoteAs") is not None: config = BuildConfig.build_bgp( request.form.get("neighborId"), request.form.get("remoteAs"), request.form.get("softReconfig"), request.form.get("nextHop"), policy=[request.form.get("direction"), request.form.get("policy")]) status = BuildConfig.send_config(device, username, password, config) DbOps.update_configurations(device, username, config, 'Bgp') # Proccess for data from input field TEXT AREA if request.form.get("bgpCustomForm") is not None: status = BuildConfig.send_config(device, username, password, request.form.get("bgpCustomForm")) DbOps.update_configurations(device, username, request.form.get("bgpCustomForm"), 'Bgp') elif request.form.get("ospfCustomForm") is not None: status = BuildConfig.send_config(device, username, password, request.form.get("ospfCustomForm")) DbOps.update_configurations(device, username, request.form.get("ospfCustomForm"), 'Ospf') return status
def get_qos_interfaces(): """Gets one interface with policies, queues, and stats""" policies = collections.defaultdict(list) # Deletes table data DbOps.delete_rows('InterfaceQos_front_end', device) make_ints_lists = is_instance(get_stats()) for i in make_ints_lists: if i.get("diffserv-target-entry", {}).get("direction", {}): queues = collections.defaultdict(list) policy_detials = { 'Policy_name': i.get('diffserv-target-entry', {}).get('policy-name', {}), 'Direction': i.get('diffserv-target-entry', {}).get('direction', {}) } policies[i['name']].append(policy_detials) for index, stat in enumerate( i.get("diffserv-target-entry", {}).get("diffserv-target-classifier-statistics", {})): # Creates list and resets at each iteration queue = { 'queue_name': stat.get('classifier-entry-name', {}), 'rate': stat.get("classifier-entry-statistics", {}).get("classified-rate", {}), 'bytes': stat.get('classifier-entry-statistics', {}).get('classified-bytes', {}), 'packets': stat.get('classifier-entry-statistics', {}).get('classified-pkts', {}), 'out_bytes': stat.get('queuing-statistics', {}).get('output-bytes', {}), 'out_packets': stat.get('queuing-statistics', {}).get('output-pkts', {}), 'drop_packets': stat.get('queuing-statistics', {}).get('drop-pkts', {}), 'drop_bytes': stat.get('queuing-statistics', {}).get('drop-bytes', {}), 'wred_drops_pkts': stat.get('queuing-statistics', {}).get('wred-stats', {}).get('early-drop-pkts', {}), 'wred_drop_bytes': stat.get('queuing-statistics', {}).get('wred-stats', {}).get('early-drop-bytes', {}) } # Write dictionary values to list, add string formatting # Write list as value to key which is our policy name queues['queues'].append(queue) policies[i['name']].append(queues) # Read policy dictionaries and write to database for k, v in policies.items(): for stat in v[1]['queues']: DbOps.update_qos_table(device, k, v[0]['Policy_name'], v[0]['Direction'], stat['queue_name'], stat['rate'], stat['bytes'], stat['packets'], stat['out_bytes'], stat['out_packets'], stat['drop_packets'], stat['drop_bytes'], stat['wred_drops_pkts'], stat['wred_drop_bytes'])
def get_trunk_ports(ssh_port, username, password, device): """Get trunk ports. Uses NETCONF and Netmiko """ trunks = [] # Deletes table data DbOps.delete_rows('Trunks_front_end', device) for ints in interface_types: try: current_interfaces = get_config()[0]["native"]["interface"].get( ints) make_list = is_in_list(current_interfaces) for interface in make_list: if interface is None: continue if interface.get("switchport", {}).get("trunk", {}).get("allowed", {}).get("vlan", {}).get("vlans", {}): # Use netconf interface name to get vlans using netmiko. I find netconf can be untrustworthy sometimes vlans = GetWithNetmiko.indivisual_poll( username, password, device, ssh_port, 'trunk_helper', interface=ints + interface.get('name')) # Write dictionary to list and join our vlan list returned from netmiko funtion trunks.append({ 'interface': ints + interface.get('name'), 'vlans': ', '.join(vlans), 'cdp': interface.get('name') }) elif interface.get("switchport", {}).get("trunk", {}).get("allowed", {}).get("vlan", {}).get("add", {}): # Use netconf interface name to get vlans using netmiko. I find netconf can be untrustworthy sometimes vlans = GetWithNetmiko.indivisual_poll( username, password, device, ssh_port, 'trunk_helper', interface=ints + interface.get('name')) # Write dictionary to list and join our vlan list returned from netmiko funtion trunks.append({ 'interface': ints + interface.get('name'), 'vlans': ', '.join(vlans), 'cdp': interface.get('name') }) except TypeError: pass # Iterate through trunk list and interface state. Comapre interface banes and get interface state. Write to database for interface in trunks: for port in get_stats(): if interface.get('interface') == port.get('name'): DbOps.update_trunks_table(device, interface['interface'], interface['vlans'], port.get('admin-status'), port.get('oper-status'))