def modalSpecificInterfaceOnHost(x, y): """Show specific interface details from device. x = device id y = interface name """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) # Removes dashes from interface in URL, replacing '_' with '/' interface = interfaceReplaceSlash(y) # Replace's '_' with '.' host.interface = interface.replace('=', '.') intConfig, intMac, intStats = host.pull_interface_info(activeSession) macToIP = '' writeToLog('viewed interface %s on host %s' % (interface, host.hostname)) return render_template("/viewspecificinterfaceonhost.html", host=host, interface=interface, intConfig=intConfig, intMac=intMac, macToIP=macToIP, intStats=intStats)
def modalEditInterfaceOnHost(x, y): """Display modal to edit specific interface on device. x = device id y = interface name """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) # Removes dashes from interface in URL interface = interfaceReplaceSlash(y) intConfig = phi.pullInterfaceConfigSession(activeSession, interface, host) # Edit form form = EditInterfaceForm(request.values, host=host, interface=interface) if form.validate_on_submit(): flash('Interface to edit - "%s"' % (interface)) return redirect('/confirm/confirmintedit') return render_template("/editinterface.html", hostid=host.id, interface=interface, intConfig=intConfig, form=form)
def confirmHostDelete(x): """Confirm deleting device interface from local database. x = device ID """ host = db_modifyDatabase.getHostByID(x) return render_template("confirm/confirmhostdelete.html", host=host)
def resultsMultiIntEdit(x, y): """Display results from editing multiple device interfaces. WIP. x = device id y = interfaces separated by '&' in front of each interface name """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = [] # Split by interfaces, separated by '&' for a in y.split('&'): if a: # Removes dashes from interface in URL a = interfaceReplaceSlash(a) result.append(host.save_config_on_device(activeSession)) writeToLog('edited multiple interfaces on host %s' % (host.hostname)) return render_template("results/resultsmultipleintedit.html", host=host, interfaces=y, result=result)
def modalSpecificInterfaceOnHost(x, y): initialChecks() # x = device id, y = interface name host = db_modifyDatabase.getHostByID(x) # Removes dashes from interface in URL, replacing '_' with '/' interface = interfaceReplaceSlash(y) # Replace's '_' with '.' interface = interface.replace('=', '.') activeSession = retrieveSSHSession(host) if host.ios_type == 'cisco_ios': intConfig, intMac = phi.pullInterfaceInfo(activeSession, interface, host) intStats = phi.pullInterfaceStats(activeSession, interface, host) macToIP = '' elif host.ios_type == 'cisco_nxos': intConfig, intMac = phi.pullInterfaceInfo(activeSession, interface, host) intStats = phi.pullInterfaceStats(activeSession, interface, host) macToIP = '' else: intConfig = phi.pullInterfaceConfigSession(activeSession, interface, host) intMac = '' macToIP = '' intStats = '' writeToLog('viewed interface %s on host %s' % (interface, host.hostname)) return render_template("/viewspecificinterfaceonhost.html", host=host, interface=interface, intConfig=intConfig, intMac=intMac, macToIP=macToIP, intStats=intStats)
def resultsMultiIntEdit(x, y): initialChecks() # x = device id, y = interfaces separated by '&' in front of each interface name host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = [] # Split by interfaces, separated by '&' for a in y.split('&'): if a: # Removes dashes from interface in URL a = interfaceReplaceSlash(a) #result.append(ci.editInterface(activeSession, a, datavlan, voicevlan, other, host)) result.append(host.save_config_on_device(activeSession)) writeToLog('edited multiple interfaces on host %s' % (host.hostname)) return render_template("results/resultsmultipleintedit.html", host=host, interfaces=y, result=result) ##################################### # End Multiple Interface Selections # #####################################
def resultsIntEdit(x, y, datavlan, voicevlan, other): """Display results for editing specific interface config settings. x = device id y = interface name d = data vlan v = voice vlan o = other """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) # Remove dashes from interface in URL and edit interface config result = host.run_edit_interface_cmd(interfaceReplaceSlash(y), datavlan, voicevlan, other, activeSession) writeToLog('edited interface %s on host %s' % (y, host.hostname)) return render_template("results/resultsinterfaceedit.html", host=host, interface=y, datavlan=datavlan, voicevlan=voicevlan, other=other, result=result)
def resultsFWACLGenerator(): try: host = db_modifyDatabase.getHostByID(session['HOSTID']) except: return redirect('/fwaclgenerator') changeTicket = session['CHANGETICKET'] changeDesc = session['CHANGEDESC'] sourceIP = session['SOURCEIP'] destIP = session['DESTIP'] ports = session['PORTS'] resultStatus, configList, backoutList, allAccessAllowed = fop.main( host, changeTicket, changeDesc, sourceIP, destIP, ports) session.pop('HOSTNAME', None) session.pop('HOSTID', None) session.pop('CHANGETICKET', None) session.pop('CHANGEDESC', None) session.pop('SOURCEIP', None) session.pop('DESTIP', None) session.pop('PORTS', None) return render_template("results/resultsfwaclgenerator.html", resultStatus=resultStatus, configList=configList, backoutList=backoutList, allAccessAllowed=allAccessAllowed)
def confirmIntDisable(x, y): # x = device id, y = interface name host = db_modifyDatabase.getHostByID(x) # Removes dashes from interface in URL y = interfaceReplaceSlash(y) return render_template("confirm/confirmintdisable.html", host=host, interface=y)
def confirmCfgCmdCustom(): host = db_modifyDatabase.getHostByID(request.form['hostid']) session['HOSTNAME'] = request.form['hostname'] session['COMMAND'] = request.form['command'] session['HOSTID'] = request.form['hostid'] session['IOS_TYPE'] = host.ios_type return render_template("confirm/confirmcfgcmdcustom.html")
def modalCmdShowCDPNeigh(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = host.pull_cdp_neighbor(activeSession) writeToLog('viewed CDP neighbors via button on host %s' % (host.hostname)) return render_template("/cmdshowcdpneigh.html", host=host, result=result)
def deviceUptime(x): """Get uptime of selected device. x = host id. """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) return jsonify(host.pull_device_uptime(activeSession))
def enterConfigMode(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) host.enter_config_mode(activeSession) writeToLog('entered config mode via iShell on host %s' % (host.hostname)) return ('', 204)
def modalCmdShowVersion(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = host.pull_version(activeSession) writeToLog('viewed version info via button on host %s' % (host.hostname)) return render_template("/cmdshowversion.html", host=host, result=result)
def modalCmdSaveConfig(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = host.save_config_on_device(activeSession) writeToLog('saved config via button on host %s' % (host.hostname)) return render_template("/cmdsaveconfig.html", host=host)
def modalCfgCmdCustom(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) # Custom Commands form form = CustomCfgCommandsForm(request.values, hostname=host.hostname) return render_template("/cfgcmdcustom.html", host=host, form=form)
def confirmMultiIntEdit(x, y): # x = device id # y = interfaces separated by '&' in front of each interface name #y = interfaceReplaceSlash(y) host = db_modifyDatabase.getHostByID(x) return render_template("confirm/confirmmultipleintedit.html", hostid=x, interfaces=y, host=host)
def hostShellOutput(x, m, y): """Display iShell output fields. x = device id m = config or enable mode y = encoded commands from javascript """ initialChecks() output = [] configError = False host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) # Decode command in URL received from javascript command = unquote_plus(y).decode('utf-8') # Replace '_' with '/' command = interfaceReplaceSlash(command) # Append prompt and command executed to beginning of output output.append(host.find_prompt_in_session(activeSession) + command) # Check if last character is a '?' if command[-1] == '?': if m == 'c': # Get command output as a list. # Insert list contents into 'output' list. configError = True else: # Get command output as a list. # Insert list contents into 'output' list. output.extend(getCmdOutputNoCR(activeSession, command)) # Append prompt and command executed to end of output output.append(host.find_prompt_in_session(activeSession)) else: if m == 'c': # Get command output as a list. # Insert list contents into 'output' list. output.extend(getCfgCmdOutput(activeSession, command)) else: # Get command output as a list. # Insert list contents into 'output' list. output.extend(host.get_cmd_output(command, activeSession)) # Append prompt and command executed to end of output. output.append(host.find_prompt_in_session(activeSession)) writeToLog('ran command on host %s - %s' % (host.hostname, command)) return render_template("hostshelloutput.html", output=output, command=command, mode=m, configError=configError)
def modalCmdShowStartConfig(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) hostConfig = host.pull_start_config(activeSession) writeToLog('viewed startup-config via button on host %s' % (host.hostname)) return render_template("/cmdshowstartconfig.html", host=host, hostConfig=hostConfig)
def confirmMultiIntEdit(x, y): """Confirm editing multiple device interfaces. WIP. x = device id y = interfaces separated by '&' in front of each interface name """ host = db_modifyDatabase.getHostByID(x) return render_template("confirm/confirmmultipleintedit.html", host=host, interfaces=y)
def hostShell(x): initialChecks() # x = device id host = db_modifyDatabase.getHostByID(x) # Exit config mode if currently in it on page refresh/load exitConfigMode(host.id) writeToLog('accessed interactive shell on host %s' % (host.hostname)) return render_template("hostshell.html", host=host)
def viewSpecificHost(x): # x is host.id initialChecks() # This fixes page refresh issue when clicking on a Modal that breaks DataTables if 'modal' in x: # Return empty response, as the page is loaded from the Modal JS # However this breaks the Loading modal JS function. Unsure why, need to research return ('', 204) host = db_modifyDatabase.getHostByID(x) writeToLog('accessed host %s' % (host.hostname)) # Get any existing SSH sessions activeSession = retrieveSSHSession(host) if host.ios_type == 'cisco_nxos': interfaces = phi.pullHostInterfacesNXOS(host.ipv4_addr, activeSession) #uptime = getCmdOutput(host.ipv4_addr, host.ios_type, 'show version | include uptime') #for x in uptime: # uptimeOutput = x.split(' ', 3)[-1] elif host.ios_type == 'cisco_ios' or host.ios_type == 'cisco_iosxe': interfaces = phi.pullHostInterfacesIOS(host.ipv4_addr, activeSession) #uptime = getCmdOutput(host.ipv4_addr, host.ios_type, 'show version | include uptime') #for x in uptime: # uptimeOutput = x.split(' ', 3)[-1] elif host.ios_type == 'cisco_asa': interfaces = phi.pullHostInterfacesASA(host.ipv4_addr, activeSession) #uptime = getCmdOutput(host.ipv4_addr, host.ios_type, 'show version | include up ') #for x in uptime: # if 'failover' in x: # break # else: # uptimeOutput = x.split(' ', 2)[-1] upInt, downInt, disabledInt, totalInt = phi.countInterfaceStatus( interfaces, host.ios_type) # if interfaces is x.x.x.x skipped - connection timeout, throw error page redirect if fn.containsSkipped(interfaces): disconnectFromSSH(ssh[sshKey]) return redirect(url_for('noHostConnectError', host=host.hostname)) elif not interfaces: disconnectFromSSH(ssh[sshKey]) return redirect(url_for('noHostConnectError', host=host.hostname)) else: return render_template("/db/viewspecifichost.html", host=host, interfaces=interfaces, upInt=upInt, downInt=downInt, disabledInt=disabledInt, totalInt=totalInt)
def confirmHostEdit(x): """Confirm settings to edit host with in local database. x = original host ID """ originalHost = db_modifyDatabase.getHostByID(x) hostname = request.form['hostname'] ipv4_addr = request.form['ipv4_addr'] hosttype = request.form['hosttype'] ios_type = request.form['ios_type'] # If exists, disconnect any existing SSH sessions # and clear them from the SSH dict try: disconnectSpecificSSHSession(originalHost) writeToLog( 'disconnected and cleared saved SSH session information for edited host %s' % (originalHost.hostname)) except (socket.error, EOFError): writeToLog('no existing SSH sessions for edited host %s' % (originalHost.hostname)) except: writeToLog('could not clear SSH session for edited host %s' % (originalHost.hostname)) result = db_modifyDatabase.editHostInDatabase(originalHost.id, hostname, ipv4_addr, hosttype, ios_type) if result: updatedHost = db_modifyDatabase.getHostByID(x) writeToLog('edited host %s in database' % (originalHost.hostname)) return render_template("confirm/confirmhostedit.html", title='Edit host confirm', originalHost=originalHost, updatedHost=updatedHost, hostname=hostname, ipv4_addr=ipv4_addr, hosttype=hosttype, ios_type=ios_type) else: return redirect(url_for('confirmHostEdit', x=originalHost))
def editHost(x): # x is host ID host = db_modifyDatabase.getHostByID(x) form = EditHostForm() if form.validate_on_submit(): return redirect('/confirm/confirmhostedit') return render_template('/edithost.html', title='Edit host in database', id=x, originalHost=host.hostname, form=form)
def modalCmdSaveConfig(x): initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) hostConfig = saveConfigOnSession(activeSession, host) writeToLog('saved config via button on host %s' % (host.hostname)) return render_template("/cmdsaveconfig.html", host=host)
def confirmIntDisable(x, y): """Confirm disabling specific device interface before executing. x = device id y = interface name """ host = db_modifyDatabase.getHostByID(x) # Removes dashes from interface in URL y = interfaceReplaceSlash(y) return render_template("confirm/confirmintdisable.html", host=host, interface=y)
def modalCmdShowInventory(x): """Display modal with device inventory information. x = device id """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) result = host.pull_inventory(activeSession) writeToLog('viewed inventory info via button on host %s' % (host.hostname)) return render_template("/cmdshowinventory.html", host=host, result=result)
def modalCfgCmdCustom(x): """Display modal to retrieve custom bulk config commands to execute. x = device id """ initialChecks() host = db_modifyDatabase.getHostByID(x) # Custom Commands form form = CustomCfgCommandsForm(request.values, hostname=host.hostname) return render_template("/cfgcmdcustom.html", host=host, form=form)
def modalCmdSaveConfig(x): """Save device configuration to memory and display result in modal. x = device id """ initialChecks() host = db_modifyDatabase.getHostByID(x) activeSession = retrieveSSHSession(host) host.save_config_on_device(activeSession) writeToLog('saved config via button on host %s' % (host.hostname)) return render_template("/cmdsaveconfig.html", host=host)
def resultsHostDeleted(x): # x = device ID host = db_modifyDatabase.getHostByID(x) # Removes host from database result = db_modifyDatabase.deleteHostInDB(host.id) if result: disconnectSpecificSSHSession(host) writeToLog('deleted host %s in database' % (host.hostname)) return render_template("results/resultshostdeleted.html", host=host, result=result) else: return redirect(url_for('confirmHostDelete', x=host.id))