Example #1
0
def resultsMultipleHostDelete(x):
    """Display results from deleting multiple devices in local databse.

    x = each host id to be deleted, separated by an '&' symbol
    """
    initialChecks()

    hostList = []
    for x in x.split('&'):
        if x:
            host = datahandler.getHostByID(x)
            hostList.append(host)
            datahandler.deleteHostInDB(x)
            try:
                sshhandler.disconnectSpecificSSHSession(host)
                logger.write_log(
                    'disconnected any remaining active sessions for host %s' %
                    (host.hostname))
            except:
                logger.write_log(
                    'unable to attempt to disconnect host %s active sessions' %
                    (host.hostname))

    overallResult = True
    return render_template("results/resultsmultiplehostdeleted.html",
                           overallResult=overallResult,
                           hostList=hostList)
Example #2
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))
Example #3
0
def callDisconnectSpecificSSHSession(x):
    """Disconnect any SSH sessions for a specific host from all users.

    x = ID of host to disconnect.
    """
    host = datahandler.getHostByID(x)
    # Disconnect device.
    try:
        sshhandler.disconnectSpecificSSHSession(host)
    except:
        # Log error if unable to disconnect specific SSH session
        logger.write_log(
            'unable to disconnect SSH session to provided host %s from user %s'
            % (host.hostname, session['USER']))
    return redirect(url_for('viewHosts'))
Example #4
0
def resultsHostDeleted(x):
    """Display results for deleting device from local database.

    x = device ID
    """
    host = datahandler.getHostByID(x)
    if host:
        # Removes host from database
        result = datahandler.deleteHostInDB(host.id)
        if result:
            sshhandler.disconnectSpecificSSHSession(host)
            return render_template("results/resultshostdeleted.html",
                                   host=host,
                                   result=result)
        else:
            return redirect(url_for('confirmHostDelete', x=host.id))
    else:
        return redirect(url_for('index'))
Example #5
0
def resultsHostEdit(x):
    """Confirm settings to edit host with in local database.

    x = original host ID
    """
    if 'modal' in x:
        return ('', 204)

    storedHost = datahandler.getHostByID(x)
    # Save all existing host variables, as the class stores get updated later in the function
    origHostname = storedHost.hostname
    origIpv4_addr = storedHost.ipv4_addr
    origHosttype = storedHost.type
    origIos_type = storedHost.ios_type
    origLocal_creds = storedHost.local_creds

    # Save form user inputs into new variables
    hostname = request.form['hostname']
    ipv4_addr = request.form['ipv4_addr']
    hosttype = request.form['hosttype']
    ios_type = request.form['ios_type']
    if request.form['local_creds'] == 'True':
        local_creds = True
        local_creds_updated = True
    elif request.form['local_creds'] == 'False':
        local_creds = False
        local_creds_updated = True
    else:
        local_creds = ''
        local_creds_updated = False

    # If exists, disconnect any existing SSH sessions
    #  and clear them from the SSH dict
    try:
        sshhandler.disconnectSpecificSSHSession(storedHost)
        logger.write_log(
            'disconnected and cleared saved SSH session information for edited host %s'
            % (storedHost.hostname))
    except (socket.error, EOFError):
        logger.write_log('no existing SSH sessions for edited host %s' %
                         (storedHost.hostname))
    except:
        logger.write_log('could not clear SSH session for edited host %s' %
                         (storedHost.hostname))

    result = datahandler.editHostInDatabase(storedHost.id, hostname, ipv4_addr,
                                            hosttype, ios_type, local_creds,
                                            local_creds_updated)

    if result:
        logger.write_log('edited host %s in database' % (storedHost.hostname))
        return render_template("results/resultshostedit.html",
                               title='Edit host confirm',
                               storedHost=storedHost,
                               hostname=hostname,
                               ipv4_addr=ipv4_addr,
                               hosttype=hosttype,
                               ios_type=ios_type,
                               local_creds=local_creds,
                               local_creds_updated=local_creds_updated,
                               origHostname=origHostname,
                               origIpv4_addr=origIpv4_addr,
                               origHosttype=origHosttype,
                               origIos_type=origIos_type,
                               origLocal_creds=origLocal_creds)
    else:
        return redirect(url_for('confirmHostEdit', x=storedHost))