def post_neighbor(): """Submit BGP for configuration""" check_for_addr_fam = GetInfo.check_for_addr_family(netmiko_session) build_neighbors = Build_bgp_config.Templates(request.form.get("localAs")) bgp_config = build_neighbors.build_neighbor( check_for_addr_fam, request.form.get("neighborId"), request.form.get("remoteAs"), model, request.form.get("softReconfig"), request.form.get("nextHop"), policy=[request.form.get("direction"), request.form.get("policy")]) status = SendConfig.send_configuration(netconf_session, bgp_config) if status == 'Success': get_status = GetInfo.get_bgp_status(netmiko_session) return jsonify({ 'data': render_template('bgp_neighbor_table.html', bgp=get_status[0]) }) else: return jsonify( {'data': render_template('config_failed.html', status=status)})
def ping(source): """Renders ping form and sends ping""" if not request.form: return render_template('ping.html', source=source.replace('%2f', '/'), vrfs=QueryDbFor.query_vrfs(device)) else: if request.form.get('vrf') == 'none': ping = GetNetmiko.send_ping(netmiko_session, netmiko_session, username, password, device, request.form.get('dest'), request.form.get('source'), request.form.get('count')) else: ping = GetNetmiko.send_ping(netmiko_session, username, password, device, request.form.get('dest'), request.form.get('source'), request.form.get('count'), vrf=request.form.get('vrf')) return render_template('ping.html', source=source.replace('%2f', '/'), vrfs=QueryDbFor.query_vrfs(device), results=ping)
def table_refresh(): """Used for table refreshes""" action = request.form.get('action') # Used for refreshing tables without page reload, return data to call wich is js/ajax if action == 'arp': clear = GetInfo.clear_arp(netmiko_session) return jsonify({'data': render_template('refresh_arp.html', arps=clear)}) elif action == 'bgp': get_status = GetInfo.get_bgp_status(netmiko_session) return jsonify({'data': render_template('refresh_bgp.html', bgp=get_status[0])}) elif action == 'ospf': get_status = GetInfo.get_ospf_status(netmiko_session) return jsonify({'data': render_template('refresh_ospf.html', ospf=get_status)}) elif action == 'clearInt': clear = GetInfo.clear_counters(netmiko_session, request.form.get('interface'), netconf_session) return jsonify({'data': render_template('refresh_table.html', interfaces=clear)}) elif action == 'routes': # ReAuth and get IOS-XE routing table routing_session = ConnectWith.creat_netmiko_connection(username, password, device) mydb = sqlite3.connect("app/Modules/ProjectRouting/Database/Routing") cursor = mydb.cursor() db_obj = DB.RoutingDatabase(mydb, cursor) IOSXE.RoutingIos(routing_session, db_obj, mydb, cursor) return jsonify({'data': render_template('get_routing.html', route_table=Db_queries.view_routes_ios(cursor))})
def modify_inteface(interface): """Renders and build configurations for interface modification""" if not request.form: current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_interface.html', interface=interface.replace('%2f', '/'), vrfs=QueryDbFor.query_vrfs(device), current_config=current_config, mgmt_int=[ i for i in QueryDbFor.query_interfaces(device) if i.id == device ], model=model[:3][-2:]) else: status = BuildConfig.parse_int_form(request.form, device, password, username) current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_interface.html', interface=interface.replace('%2f', '/'), vrfs=QueryDbFor.query_vrfs(device), mgmt_int=[ i for i in QueryDbFor.query_interfaces(device) if i.id == device ], current_config=current_config, model=model[:3][-2:], status=status)
def submit_ping(): """Render OSPF configuration/Form""" if request.form.get('vrf') == 'none': ping = GetInfo.send_ping(netmiko_session, request.form.get('dest'), request.form.get('source'), request.form.get('count')) else: ping = GetInfo.send_ping(netmiko_session, request.form.get('dest'), request.form.get('source'), request.form.get('count'), vrf=request.form.get('vrf')) return jsonify({'data': render_template('ping_status.html', results=ping)})
def modify_inteface(interface): """POST BGP configuration from form data""" reformat_interface = interface.replace('%2f', '/') vrfs = GetInfo.get_vrfs(netmiko_session) return render_template('modify_interface.html', interface=reformat_interface, vrfs=vrfs, mgmt_int=management_int)
def ping(source): """Render OSPF configuration/Form""" reformat_interface = source.replace('%2f', '/') vrfs = GetInfo.get_vrfs(netmiko_session) return render_template('ping.html', source=reformat_interface, vrfs=vrfs)
def interface_details(): """Used for table refreshes""" int_details = GetInfo.more_int_details(netmiko_session, request.form.get('details')) return render_template('more_int_detials.html', details=int_details)
def add_poch(): """Renders vlan configuration form""" # Remove duplicate channel-group numbers for html dropdown groups = [i.group for i in QueryDbFor.query_port_channels(device)] if not request.form: return render_template( 'add_poch.html', interfaces=QueryDbFor.query_interfaces(device), port_channels=list(dict.fromkeys(groups)), int_status=QueryDbFor.query_access_ports(device)) else: channel_mode = [ i.mode for i in QueryDbFor.query_port_channels(device) if request.form.get('pochannel') == i.group ] status = BuildConfig.parse_poch_interface( request.form, list(dict.fromkeys(channel_mode)), device, password, username) current_config = GetNetmiko.current_int_config( netmiko_session, request.form.get('interface')) return render_template( 'add_poch.html', interfaces=QueryDbFor.query_interfaces(device), port_channels=groups, int_status=QueryDbFor.query_access_ports(device), current_config=current_config, status=status)
def cdp_interface_details(): """Get cdp neighbor detail""" int_details = GetInfo.get_cdp_neighbors_detail(netmiko_session, request.form.get('details')) return render_template('more_int_detials.html', details=int_details)
def span_details(): """Get spanning tree vlan details""" int_details = GetInfo.get_span_detail(netmiko_session, request.form.get('details')) return render_template('more_int_detials.html', details=int_details)
def get_routing(): """Gets all things routing, arp, interfaces, routing protocols""" global get_interfaces, local_as, netconf_session, management_int, unassigned_ints, interface_nums netconf_session = ConnectWith.create_netconf_connection(username, password, device) get_interfaces = GetInterfacesInfo.get_ip_interfaces(netconf_session, device) management_int = get_interfaces[1] unassigned_ints = get_interfaces[2] interface_nums = get_interfaces[3] bgp_status = GetInfo.get_bgp_status(netmiko_session) local_as = bgp_status[1][0] ospf_status = GetInfo.get_ospf_status(netmiko_session) arp_table = GetInfo.get_arp(netmiko_session) return render_template('routing.html', interfaces=get_interfaces[0], bgp=bgp_status[0], ospf=ospf_status, arp=arp_table, intial='yes')
def add_ospf_neighbors(): """Render OSPF configuration/Form""" global ospf_processes ospf_processes = GetInfo.get_ospf_processes(netmiko_session) return render_template('add_ospf_neighbor.html', ospf_proc=ospf_processes)
def new_interface(): """POST BGP configuration from form data""" vrfs = GetInfo.get_vrfs(netmiko_session) return render_template('new_int_form.html', interfaces=unassigned_ints, vrfs=vrfs, interface_numbers=interface_nums)
def layer_2(): """Gets layer two information from the device""" cdp = GetInfo.get_cdp_neighbors(netmiko_session) vlans = GetInfo.get_netmiko_vlans(netmiko_session) spanning_tree = GetInfo.get_span_root(netmiko_session) mac_to_arp = GetInfo.get_mac_arp_table(netmiko_session) trunks = GetInterfacesInfo.get_trunk_ports(netconf_session) access_ports = GetInterfacesInfo.get_access_ports(netconf_session) port_channels = GetInterfacesInfo.get_port_channels(netconf_session) return render_template('get_layer_two.html', neighbors=cdp, vlans=vlans, trunks=trunks, access_ports=access_ports, port_chan=port_channels, roots=spanning_tree, mac_arp=mac_to_arp)
def route_details(): """Gets ip route details, CLI view""" details = request.form.get('details').split('-') route_details = GetNetmiko.get_route_detials(netmiko_session, details[0].split('/')[0], details[1]) return render_template('route_detials.html', route=route_details[0], route_detials=route_details[1])
def route_details(): """Used for table refreshes""" details = request.form.get('details').split('-') route_details = GetInfo.get_route_detials(netmiko_session, details[0].split('/')[0], details[1]) return render_template('route_detials.html', route=route_details[0], route_detials=route_details[1])
def hsrp_details(): """Get HSRP details, CLI view""" # Get HSRP details. Strip 'HSRP' off the JS Value. details = GetNetmiko.get_hsrp_detail( netmiko_session, request.form.get('details').strip('HSRP')) return render_template('more_int_detials.html', hsrp_int=details[0], hsrp_neighbor=details[1])
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 login(): global device, username, password, netconf_session, netmiko_session, model, netconf_port, ssh_port login_form = LoginForm(request.form) if 'login' in request.form: device = request.form['device'] username = request.form['username'] password = request.form['password'] netconf_port = request.form['netconf'] ssh_port = request.form['ssh'] if not netconf_port: netconf_port = 830 if not ssh_port: ssh_port = 22 if device and username and password: # 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) model = GetInfo.get_device_model(netmiko_session) # 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.login')) elif netmiko_session == 'ssh_exception' or netmiko_session == 'Connection Timeout': flash("Check Device Connectivity") return redirect(url_for('base_blueprint.login')) if netconf_session == 'Authentication Error': flash("Authentication Failure") return redirect(url_for('base_blueprint.login')) elif netconf_session == 'Connection Timeout' or netconf_session == 'Connectivity Issue': flash("Check Device Connectivity") return redirect(url_for('base_blueprint.login')) else: return redirect(url_for('base_blueprint.get_routing')) return render_template('accounts/login.html', msg='Wrong user or password', form=login_form) if not current_user.is_authenticated: return render_template('accounts/login.html', form=login_form) return redirect(url_for('home_blueprint.index'))
def modify_access_interface(interface): """POST BGP configuration from form data""" if not request.form: current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_access_int.html', interface=interface.replace('%2f', '/'), current_config=current_config, vlans=QueryDbFor.query_vlans(device)) else: status = BuildConfig.parse_access_int_form(request.form, device, password, username) current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_access_int.html', interface=interface.replace('%2f', '/'), current_config=current_config, vlans=QueryDbFor.query_vlans(device), status=status)
def modify_trunk_interface(interface): """Render trunk modification page""" if not request.form: current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_trunk_int.html', interface=interface.replace('%2f', '/'), current_config=current_config, vlans=QueryDbFor.query_vlans(device)) else: status = BuildConfig.parse_trunk_interface(request.form, device, password, username) current_config = GetNetmiko.current_int_config( netmiko_session, interface.replace('%2f', '/')) return render_template('modify_trunk_int.html', interface=interface.replace('%2f', '/'), current_config=current_config, vlans=QueryDbFor.query_vlans(device), status=status)
def modify_inteface(interface): """POST BGP configuration from form data""" reformat_interface = interface.replace('%2f', '/') vrfs = GetInfo.get_vrfs(netmiko_session) mac = GetInterfacesInfo.get_single_interfaces(netconf_session, reformat_interface) return render_template('modify_interface.html', interface=reformat_interface, vrfs=vrfs, mgmt_int=management_int, mac=mac[0].get(reformat_interface).get('MAC'))
def new_dmvpn_interface(): """POST new interface""" if not request.form: return render_template('new_dmvpn_int.html', vrfs=QueryDbFor.query_vrfs(device)) else: status = BuildConfig.parse_int_form(request.form, device, password, username) interface = request.form.get('newIntCustomForm').splitlines() current_config = GetNetmiko.current_int_config(netmiko_session, interface[0].split()[1]) return render_template('new_dmvpn_int.html')
def post_ospf_neighbor(): """POST OSPF configuration from form data""" build_neighbors = Build_ospf_config.Templates(request.form.get("process")) ospf_config = build_neighbors.build_neighbor(request.form.get("neighbor"), request.form.get("wildcard"), request.form.get("area")) status = SendConfig.send_configuration(netconf_session, ospf_config) if status == 'Success': get_status = GetInfo.get_ospf_status(netmiko_session) return jsonify({'data': render_template('ospf_neighbor_table.html', ospf=get_status)}) else: return jsonify({'data': render_template('config_failed.html', status=status)})
def table_refresh(): """Used for table refreshes""" action = request.form.get('action') # Used for refreshing tables without page reload, return data to call wich is js/ajax if action == 'arp': clear = GetInfo.clear_arp(netmiko_session) return jsonify( {'data': render_template('refresh_arp.html', arps=clear)}) elif action == 'bgp': get_status = GetInfo.get_bgp_status(netmiko_session) return jsonify( {'data': render_template('refresh_bgp.html', bgp=get_status[0])}) elif action == 'ospf': get_status = GetInfo.get_ospf_status(netmiko_session) return jsonify( {'data': render_template('refresh_ospf.html', ospf=get_status)}) elif action == 'clearInt': clear = GetInfo.clear_counters(netmiko_session, request.form.get('interface'), netconf_session) return jsonify( {'data': render_template('refresh_table.html', interfaces=clear)}) elif action == 'mac': mac_to_arp = GetInfo.get_mac_arp_table(netmiko_session) return jsonify( {'data': render_template('refresh_mac.html', mac_arp=mac_to_arp)}) elif action == 'cdp': cdp = GetInfo.get_cdp_neighbors(netmiko_session) return jsonify( {'data': render_template('refresh_cdp.html', neighbors=cdp)}) elif action == 'access': access_ports = GetInterfacesInfo.get_access_ports(netconf_session) return jsonify({ 'data': render_template('refresh_access.html', access_ports=access_ports) }) elif action == 'span': spanning_tree = GetInfo.get_span_root(netmiko_session) return jsonify({ 'data': render_template('refresh_span.html', roots=spanning_tree) })
def layer_2(): """Gets layer two information from the device""" global vdcs vlans = GetInfo.get_vlans(netmiko_session) trunks = GetInfo.get_trunks(netmiko_session) port_channels = GetInfo.get_port_channels(netmiko_session) interfaces = GetInfo.get_interface_names(netmiko_session) arp = GetInfo.get_arp(netmiko_session) vdcs = GetInfo.get_vdcs(netmiko_session) vpcs = GetInfo.get_vpcs(netmiko_session) return render_template('get_layer_two.html', vlans=vlans, trunks=trunks, port_channels=port_channels, interfaces=interfaces, arp=arp, vdcs=vdcs[0], current_vdc=vdcs[1], vpcs=vpcs)
def add_vlan(): """Gets layer two information from the device""" interfaces = GetInfo.get_interface_names(netmiko_session) return render_template('add_vlan.html')
def vdc_layer_2(): """Gets layer two information from the device""" global vdc if vdc == None: vdc = request.form.get("vdc") GetInfo.switch_vdc(netmiko_session, vdc=request.form.get("vdc")) else: GetInfo.switch_vdc_back(netmiko_session) GetInfo.switch_vdc(netmiko_session, vdc=request.form.get("vdc")) vlans = GetInfo.get_vlans(netmiko_session) trunks = GetInfo.get_trunks(netmiko_session) port_channels = GetInfo.get_port_channels(netmiko_session) interfaces = GetInfo.get_interface_names(netmiko_session) arp = GetInfo.get_arp(netmiko_session) current_vdc = GetInfo.get_vdcs(netmiko_session) vpcs = GetInfo.get_vpcs(netmiko_session) return render_template('get_layer_two.html', vlans=vlans, trunks=trunks, port_channels=port_channels, interfaces=interfaces, arp=arp, vdcs=vdcs[0], current_vdc=current_vdc[1], vpc=vpcs)
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'))