Ejemplo n.º 1
0
def pullInterfaceMacAddressesSession(ssh, interface, host):
    output = []

    if host.ios_type == 'cisco_nxos':
        command = "show mac address-table interface %s | exclude VLAN | exclude Legend" % (
            interface)
    elif host.ios_type == 'cisco_ios' or host.ios_type == 'cisco_asa':
        command = "show mac address-table interface %s" % (interface)
    else:
        command = "show mac address-table interface %s" % (interface)

    # Loop once in case the command requires a dash between 'mac' and 'address-table'
    for a in range(2):
        # Print existing configuration settings for interface
        result = nfn.runSSHCommandInSession(command, ssh)

        if "Invalid input detected" in result and host.ios_type == 'cisco_ios':
            command = "show mac-address-table interface %s" % (interface)
            continue
        else:
            break

    # If invalid input detected both times, return blank as an error, so it won't display the mac address table (since it's empty)
    if "Invalid input detected" in result:
        return '', ssh

    result = fn.replaceDoubleSpacesCommas(result)

    result = result.replace('*', '')

    output = result.split('\n')
    return output
Ejemplo n.º 2
0
def saveConfigOnSession(ssh, host):
    output = []
    if host.ios_type == 'cisco_nxos':
        command = 'copy running-config startup-config'
    else:
        command = 'wr mem'
    result = nfn.runSSHCommandInSession(command, ssh)
    output = result.split('\n')
    return output
Ejemplo n.º 3
0
def getCmdOutput(ssh, command):
    output = []

    # Get command output from network device
    result = nfn.runSSHCommandInSession(command, ssh)

    # Split output by newline
    output = result.split('\n')

    # Return config
    return output
Ejemplo n.º 4
0
def getCmdOutputWithCommas(ssh, command):
    output = []

    # Get command output from network device
    result = nfn.runSSHCommandInSession(command, ssh)

    result = replaceDoubleSpacesCommas(result)

    # Split output by newline
    output = result.split('\n')

    # Return config
    return output
Ejemplo n.º 5
0
def pullInterfaceStats(ssh, interface, host):
    output = []

    if host.ios_type == 'cisco_nxos':
        command = "show interface %s" % (interface)
    elif host.ios_type == 'cisco_ios' or host.ios_type == 'cisco_asa':
        command = "show interface %s" % (interface)
    else:
        command = "show interface %s" % (interface)

    # Save existing interface statistics
    result = nfn.runSSHCommandInSession(command, ssh)

    output = result.split('\n')
    return output
Ejemplo n.º 6
0
def pullHostInterfacesASA(host, ssh):
    output = []

    command = "show interface ip brief"

    # Print existing configuration settings for interface
    result = nfn.runSSHCommandInSession(command, ssh)

    if not result:
        return result
    # Clean up output for table on HTML output
    result = cleanUpIOSOutput(result)

    output = result.split('\n')

    return output
Ejemplo n.º 7
0
def pullInterfaceConfigSession(ssh, interface, host):
    output = []

    if host.ios_type == 'cisco_nxos':
        command = "show run interface %s | exclude version | exclude Command | exclude !" % (
            interface)
    elif host.ios_type == 'cisco_ios' or host.ios_type == 'cisco_asa':
        command = "show run interface %s | exclude configuration|!" % (
            interface)
    else:
        command = "show run interface %s | exclude configuration|!" % (
            interface)

    # Print existing configuration settings for interface
    result = nfn.runSSHCommandInSession(command, ssh)

    output = result.split('\n')
    return output
    source = fn.replaceDoubleSpaces(source)
    # Split string by spaces.  The 1st field is the IP address, the 2nd field is the subnet mask
    sourceList = source.split(" ")

    # Needed to preserve user provided source in below loop, and be able to change these Cmd varables without affecting original user input
    # IP address
    sourceIPCmd = sourceList[0]
    # Subnet mask
    sourceMaskCmd = sourceList[1]

    # Loop once only in case initial source isn't in ASA routing table, then search for default route
    for k in range(2):
        # Command to check for interface source would originate from
        command = "show route %s | inc via" % (sourceIPCmd)
        # Get result of above command when run on the firewall
        result = nfn.runSSHCommandInSession(command, ssh)

        # This is blank if it isn't in the routing table; AKA check for default route next
        if fn.isEmpty(result):
            sourceIPCmd = "0.0.0.0"
            continue
        else:
            break

    # Split returned results by line
    result = result.split("\n")
    # Store last line of list into a new string variable
    resultStr = result[-1]
    # Reduce all spacing to just a single space per section
    resultStr = fn.replaceDoubleSpaces(resultStr)
    # Split string by spaces.  We are looking for the last field
Ejemplo n.º 9
0
def pullHostInterfacesNXOS(host, ssh):
    output = []
    outputResult = ''

    #command = "show interface status | exclude -----"
    command = "show interface status | xml"

    # Print existing configuration settings for interface
    result = nfn.runSSHCommandInSession(command, ssh)

    # Returns False if nothing was returned
    if not result:
        return result

    result = nfn.runSSHCommandInSession(command, ssh)

    result = re.findall("\<\?xml.*reply\>", result, re.DOTALL)

    # Strip namespaces
    it = ET.iterparse(StringIO(result[0]))
    for _, el in it:
        if '}' in el.tag:
            el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
    root = it.root

    # This variable is to skip the first instance of "ROW_interface" in the XML output
    a = False
    nameStatus = False
    for elem in root.iter():
        if a:
            if not elem.tag.isspace() and not elem.text.isspace():
                # This is in case there is no name/description:
                # Reset counter var to True on each loop back to a new 'interface'
                if elem.tag == 'interface':
                    nameStatus = True
                # Name input provided, set var to False to skip the '--' ahead
                elif elem.tag == 'name':
                    nameStatus = False
                # No Name column provided, use '--' instead
                elif elem.tag == 'state' and nameStatus:
                    outputResult = outputResult + 'ip,--,'

                # Skip certain columns
                if elem.tag == 'vlan' or elem.tag == 'duplex' or elem.tag == 'type':
                    pass
                # Placeholder 'ip' for upcoming IP address lookup in new function
                elif elem.tag == 'name':
                    # Truncate description (name column) to 25 characters only
                    outputResult = outputResult + 'ip,' + elem.text[:25] + ','
                # Otherwise store output to string
                else:
                    outputResult = outputResult + elem.text + ','

        # This is to skip the first instance of "ROW_interface" in the XML output
        if elem.tag == 'ROW_interface':
            outputResult = outputResult + '\n'
            a = True

    command = 'sh run int | egrep interface|ip.address | ex passive | ex !'

    result = nfn.runSSHCommandInSession(command, ssh)

    # Set intStatus var to False initially
    intStatus = 0
    # Keeps track of the name of the interface we're on currently
    currentInt = ''
    realIP = ''
    realIPList = []
    # This extracts the IP addresses for each interface, and inserts them into the outputResult string
    for x in result.split('\n'):
        #outputResult = outputResult + x + '\n'

        # Line is an interface
        if 'interface' in x:
            currentInt = x.split(' ')
            intStatus += 1

        # No IP address, so use '--' instead
        if 'interface' in x and intStatus == 2:
            # Reset counter
            intStatus = 1
            a = currentInt[1] + ',ip'
            b = currentInt[1] + ',--'
        else:
            realIPList = x.split(' ')
            realIP = realIPList[-1]
            a = currentInt[1] + ',ip'
            b = currentInt[1] + ',' + realIP
            outputResult = outputResult.replace(a, b)

    # Cleanup any remaining instances of 'ip' in outputResult
    outputResult = outputResult.replace(',ip,', ',--,')
    # Split by newlines
    output = outputResult.split('\n')

    # Return output as List
    return output