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 start_polling(username, pwd, host, device_type, port): global device, session, password, ssh_port username = username password = pwd device = host ssh_port = port session = ConnectWith.creat_netmiko_connection(username, password, device, ssh_port) if device_type[:3][-2:] != 'SR': get_mac_arp_table() get_span_root() get_access_ports() get_vlans() elif device_type[:3][-2:] == 'SR': get_dmvpn() get_dmvpn_info() get_arp() get_cdp_neighbors() get_ospf_status() get_bgp_status() get_vrfs() get_ospf_processes() get_route_maps() get_hsrp_status() get_ospf_routers()
def send_command(netmiko_connection, command, expect_string=None): """Send Netmiko commands""" netmiko_connection = netmiko_connection get_response = None if expect_string is None: retries = 0 while retries != 3: try: get_response = netmiko_connection.send_command( command_string=command) break except (OSError, TypeError, AttributeError, ssh_exception.NetmikoTimeoutException): netmiko_connection = ConnectWith.creat_netmiko_connection( Credentials.username, Credentials.password, Credentials.device) Credentials.netmiko_session = netmiko_connection retries += 1 else: retries = 0 while retries != 3: try: get_response = netmiko_connection.send_command( command_string=command, expect_string=expect_string) break except (OSError, TypeError, AttributeError, ssh_exception.NetmikoTimeoutException): netmiko_connection = ConnectWith.creat_netmiko_connection( Credentials.username, Credentials.password, Credentials.device) retries += 1 if retries == 3: get_response = 'Error Connecting' return get_response
def routing_table(): """Used for table refreshes""" routing_session = ConnectWith.creat_netmiko_connection( username, password, device, ssh_port) mydb = sqlite3.connect("app/Modules/ProjectRouting/Database/Routing") cursor = mydb.cursor() db_obj = DB.RoutingDatabase(mydb, cursor) NXOS.RoutingNexus(routing_session, db_obj, mydb, cursor) return render_template('get_routing.html', route_table=Db_queries.view_routes_nexus(cursor))
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 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 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 send_config_cli(host, username, password, config): """Send Jinja config using netmiko. Used for configs napalm wont send""" # Create netconfi session session = ConnectWith.creat_netmiko_connection(username, password, host, 22) try: session.send_command('config t\n', expect_string='#') for i in config.splitlines(): print(i) session.send_command(i + '\n', expect_string='#') status = 'custom_success' except: status = 'custom_fail' return status
def indivisual_poll(user, pwd, host, port, polling, interface=None): global session, device username = user password = pwd device = host ssh_port = port session = ConnectWith.creat_netmiko_connection(username, password, device, ssh_port) if polling == 'arp': get_arp() elif polling == 'bgp': get_bgp_status() elif polling == 'ospf': get_ospf_status() elif polling == 'mac': get_mac_arp_table() elif polling == 'cdp': get_cdp_neighbors() elif polling == 'span': get_span_root() elif polling == 'access': get_access_ports() elif polling == 'clearInt': clear_counters(interface) elif polling == 'clearArp': clear_arp() elif polling == 'refreshArp': get_arp() elif polling == 'vlans': get_vlans() elif polling == 'trunk_helper': trunks = netconf_trunk_helper(interface) return trunks elif polling == 'hsrp': get_hsrp_status() elif polling == 'peer_count': get_dmvpn() elif polling == 'borderRouters': get_ospf_routers()