Beispiel #1
0
def modalEditInterfaceOnHost(x):
    """Display modal to edit specific interface on device.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)

    activeSession = sshhandler.retrieveSSHSession(host)

    # Removes dashes from interface in URL
    # interface = interfaceReplaceSlash(y)
    # Replace's '=' with '.'
    # host.interface = interface.replace('=', '.')

    # Set interface to passed parameter in URL
    host.interface = request.args.get('int', '')

    intConfig = host.pull_interface_config(activeSession)
    # Edit form
    form = EditInterfaceForm(request.values,
                             host=host,
                             interface=host.interface)

    if form.validate_on_submit():
        flash('Interface to edit - "%s"' % (host.interface))
        return redirect('/confirm/confirmintedit')

    return render_template("/editinterface.html",
                           hostid=host.id,
                           hostinterface=host.interface,
                           intConfig=intConfig,
                           form=form)
Beispiel #2
0
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 = datahandler.getHostByID(x)
    activeSession = sshhandler.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))

    logger.write_log('edited multiple interfaces on host %s' % (host.hostname))
    return render_template("results/resultsmultipleintedit.html",
                           host=host,
                           interfaces=y,
                           result=result)
Beispiel #3
0
def modalSpecificInterfaceOnHost(x, y):
    """Show specific interface details from device.

    x = device id
    y = interface name
    """
    initialChecks()

    host = datahandler.getHostByID(x)

    activeSession = sshhandler.retrieveSSHSession(host)

    # Removes dashes from interface in URL, replacing '_' with '/'
    interface = interfaceReplaceSlash(y)
    # Replace's '=' with '.'
    host.interface = interface.replace('=', '.')

    intConfig, intMacAddr, intStats = host.pull_interface_info(activeSession)
    macToIP = ''

    logger.write_log('viewed interface %s on host %s' %
                     (host.interface, host.hostname))
    return render_template("/viewspecificinterfaceonhost.html",
                           host=host,
                           interface=interface,
                           intConfig=intConfig,
                           intMacAddr=intMacAddr,
                           macToIP=macToIP,
                           intStats=intStats)
Beispiel #4
0
def viewSpecificHost(x):
    """Display specific device page.

    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 = datahandler.getHostByID(x)

    logger.write_log('accessed host %s using IPv4 address %s' %
                     (host.hostname, host.ipv4_addr))

    # Try statement as if this page was accessed directly and not via the Local Credentials form it will fail and we want to operate normally
    # Variable to determine if successfully connected o host use local credentials
    varFormSet = False
    try:
        if storeUserInRedis(request.form['user'],
                            request.form['pw'],
                            privpw=request.form['privpw'],
                            host=host):
            # Set to True if variables are set correctly from local credentials form
            varFormSet = True
            logger.write_log(
                'local credentials saved to REDIS for accessing host %s' %
                (host.hostname))

    except:
        # If no form submitted (not using local credentials), get SSH session
        # Don't go in if form was used (local credentials) but SSH session failed in above 'try' statement
        if not varFormSet:
            logger.write_log(
                'credentials used of currently logged in user for accessing host %s'
                % (host.hostname))

    # Get any existing SSH sessions
    activeSession = sshhandler.retrieveSSHSession(host)
    result = host.pull_host_interfaces(activeSession)

    if result:
        interfaces = host.count_interface_status(result)
        return render_template("/db/viewspecifichost.html",
                               host=host,
                               interfaces=interfaces,
                               result=result)
    else:
        # If interfaces is x.x.x.x skipped - connection timeout,
        #  throw error page redirect
        sshhandler.disconnectSpecificSSHSession(host)
        return redirect(url_for('noHostConnectError', host=host))
Beispiel #5
0
def deviceUptime(x):
    """Get uptime of selected device.

    x = host id.
    """
    initialChecks()
    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    logger.write_log('retrieved uptime on host %s' % (host.hostname))
    return jsonify(host.pull_device_uptime(activeSession))
Beispiel #6
0
def devicePoeStatus(x):
    """Get PoE status of all interfaces on device.

    x = host id.
    """
    initialChecks()
    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    logger.write_log('retrieved PoE status for interfaces on host %s' %
                     (host.hostname))
    return json.dumps(host.pull_device_poe_status(activeSession))
Beispiel #7
0
def hostShellOutput(x, m, y):
    """Display iShell output fields.

    x = device id
    m = config or enable mode
    y = encoded commands from javascript
    """
    initialChecks()

    configError = False

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)

    # Replace '___' with '/'
    x = unquote_plus(y).decode('utf-8')
    command = x.replace('___', '/')
    # command = interfaceReplaceSlash(unquote_plus(y).decode('utf-8'))

    # 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
            output = ''
        else:
            # Run command on provided existing SSH session and returns output.
            # Since we set normalize to False, we need to do this.
            # The normalize() function in NetMiko does rstrip and adds a CR to the end of the command.
            output = activeSession.send_command(command.strip(),
                                                normalize=False).splitlines()

    else:
        if m == 'c':
            # Get configuration command output from network device, split output by newline
            output = activeSession.send_config_set(
                command, exit_config_mode=False).splitlines()
            # Remove first item in list, as Netmiko returns the command ran only in the output
            output.pop(0)
        else:
            output = host.get_cmd_output(command, activeSession)

    logger.write_log('ran command on host %s - %s' % (host.hostname, command))

    return render_template("hostshelloutput.html",
                           output=output,
                           command=command,
                           mode=m,
                           configError=configError)
Beispiel #8
0
def modalCmdSaveConfig(x):
    """Save device configuration to memory and display result in modal.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    host.save_config_on_device(activeSession)

    logger.write_log('saved config via button on host %s' % (host.hostname))
    return render_template("/cmdsaveconfig.html", host=host)
Beispiel #9
0
def modalCmdShowCDPNeigh(x):
    """Display modal with CDP/LLDP neighbors info for device.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    neigh = host.pull_cdp_neighbor(activeSession)
    logger.write_log('viewed CDP neighbors via button on host %s' %
                     (host.hostname))
    return render_template("/cmdshowcdpneigh.html", host=host, neigh=neigh)
Beispiel #10
0
def modalCmdShowVersion(x):
    """Display modal with device version information.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    result = host.pull_version(activeSession)

    logger.write_log('viewed version info via button on host %s' %
                     (host.hostname))
    return render_template("/cmdshowversion.html", host=host, result=result)
Beispiel #11
0
def enterConfigMode(x):
    """Enter device configuration mode.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    # Enter configuration mode on device using existing SSH session
    activeSession.config_mode()
    logger.write_log('entered config mode via iShell on host %s' %
                     (host.hostname))
    return ('', 204)
Beispiel #12
0
def modalCmdShowStartConfig(x):
    """Display modal with saved/stored configuration settings on device.

    x = device id
    """
    initialChecks()

    host = datahandler.getHostByID(x)
    activeSession = sshhandler.retrieveSSHSession(host)
    hostConfig = host.pull_start_config(activeSession)
    logger.write_log('viewed startup-config via button on host %s' %
                     (host.hostname))
    return render_template("/cmdshowstartconfig.html",
                           host=host,
                           hostConfig=hostConfig)
Beispiel #13
0
def resultsCmdCustom():
    """Display results from bulk command execution on device."""
    initialChecks()

    host = datahandler.getHostByID(session['HOSTID'])

    activeSession = sshhandler.retrieveSSHSession(host)

    command = session['COMMAND']

    result = host.run_multiple_commands(command, activeSession)

    session.pop('HOSTNAME', None)
    session.pop('COMMAND', None)
    session.pop('HOSTID', None)

    logger.write_log('ran custom commands on host %s' % (host.hostname))
    return render_template("results/resultscmdcustom.html",
                           host=host,
                           command=command,
                           result=result)
Beispiel #14
0
def resultsIntDisabled(x, y):
    """Display results for disabling specific interface.

    x = device id
    y = interface name
    """
    initialChecks()

    host = datahandler.getHostByID(x)

    activeSession = sshhandler.retrieveSSHSession(host)

    # Removes dashes from interface in URL and disable interface
    result = host.run_disable_interface_cmd(interfaceReplaceSlash(y),
                                            activeSession)

    logger.write_log('disabled interface %s on host %s' % (y, host.hostname))
    return render_template("results/resultsinterfacedisabled.html",
                           host=host,
                           interface=y,
                           result=result)
Beispiel #15
0
def resultsIntEdit(x, datavlan, voicevlan, other):
    """Display results for editing specific interface config settings.

    x = device id
    d = data vlan
    v = voice vlan
    o = other
    """
    initialChecks()

    host = datahandler.getHostByID(x)

    activeSession = sshhandler.retrieveSSHSession(host)

    # Get interface from passed variable in URL
    hostinterface = request.args.get('int', '')

    # Decode 'other' string
    other = unquote_plus(other).decode('utf-8')

    # Replace '___' with '/'
    other = other.replace('___', '/')

    # Replace '\r\n' with '\n'
    other = other.replace('\r\n', '\n')

    # Remove dashes from interface in URL and edit interface config
    result = host.run_edit_interface_cmd(hostinterface, datavlan, voicevlan,
                                         other, activeSession)

    logger.write_log('edited interface %s on host %s' %
                     (hostinterface, host.hostname))
    return render_template("results/resultsinterfaceedit.html",
                           host=host,
                           interface=hostinterface,
                           datavlan=datavlan,
                           voicevlan=voicevlan,
                           other=other,
                           result=result)