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 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'))