def findMacbyIp(addr, host): # Find what the MAC address is associated with the provided IP address # If client IP address is on a vlan hosted on a firewall, pull MAC from firewall # Currently not fully implemented yet '''if ( "10.x.x." in ipAddr or "10.x.x." in ipAddr or "10.x.x." in ipAddr ): # Below 2 lines not used; left for future ASA support showArp = "show arp | include " host = hostCoreASA asaClient = True # If client IP address is not on a firewall-hosted vlan, pull MAC from Core else: showArp = "show ip arp | include " asaClient = False''' showArp = "show ip arp | include " asaClient = False # Set command to run for ARP table lookup command = showArp + addr + "\n" # Run command, save output to 'result' if asaClient: result = sfn.runSSHCommandASA(command, host, creds) # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(result): print "Client IP address is not found on the core switch or the ASA. Error #301. Please try again" fn.debugScript('301') # Split result into list split by newlines result = result.splitlines() # Replace everywhere with multiple spaces with only a single space result = fn.replaceDoubleSpaces(result[-3]) else: result = sfn.runSSHCommand(command, host, creds) # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(result): print "Client IP address is not found in the core network. Error #302. Please try again" fn.debugScript('302') # Replace everywhere with multiple spaces with only a single space result = fn.replaceDoubleSpaces(result) # Split result by individual spaces result = result.split(" ") # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(result[2]): print "Client MAC address is not found. Please try again" sys.exit() # Otherwise, return MAC address found from ARP table else: if result[2] == "arp": print "Client IP address is identified as on the firewall but cannot be found on it. Error #303. Please try again" fn.debugScript('303') else: return result[2]
def findIpByMac(addr, host): # Find what the IP address is associated with the provided MAC address showArp = "show ip arp | include " command = showArp + addr # Run command, save output to 'result' result = sfn.runSSHCommand(command, host, creds) # If MAC address isn't in ARP table, or listed as Incomplete, check a firewall to see if its hosted there if fn.errorCheckEmptyIncResult(result): # Currently not fully implemented yet '''showArp = "show arp | include " command = showArp + addr host = hostCoreASA asaClient = True''' if asaClient: result = sfn.runSSHCommandASA(command, host, creds) # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(result): print "Client MAC address is not found on the core switch or internal ASA. Error #201. Please try again" fn.debugErrorOut('201') # Split result into list split by newlines result = result.splitlines() # Replace everywhere with multiple spaces with only a single space result = fn.replaceDoubleSpaces(result[-3]) # Split result by individual spaces result = result.split(" ") # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(result[2]): print "Client MAC address is not found. Error #202. Please try again" fn.debugScript('202') # Otherwise, return MAC address found from ARP table else: if result[2] == "arp": print "Client IP address is identified as as on the firewall but cannot be found on it. Error #203. Please try again" fn.debugScript('203') else: return result[1] else: # Replace everywhere with multiple spaces with only a single space result = fn.replaceDoubleSpaces(result) # Split result by individual spaces clientIPAddr = result.split(" ") # If MAC address isn't in ARP table, or listed as Incomplete, exit script if fn.errorCheckEmptyIncResult(clientIPAddr[0]): print "Client IP address is not found. Please try again" fn.debugScript('204') # Otherwise, return IP address found from ARP table else: return clientIPAddr[0]
# Script variables ### /Variables ### # Get credentials from user if not already set user, pw = ufn.getUserCredentials(user, pw) # Store user credentials in creds class creds = fn.setUserCredentials(user, pw) host = raw_input("What switch is the interface on? ") iface = raw_input("What interface do you want to configure? ") print "\n\nConfig settings for interface %s on %s:" % (iface, host) command = "show run int %s | ex configuration|!" % (iface) # Print existing configuration settings for interface print sfn.runSSHCommand(command, host, creds) print "\n" # Loop menu in case invalid option is selected while True: # Loop menu in case incorrect option is selected, and user declines confirming menu option while True: print "\n" print "Troubleshooting Menu for interface %s\n" % (iface) print "1) Bounce port" print "2) Shutdown port" print "3) Enable port" print "4) Change data vlan on port" print "5) Change voice vlan on port" print "6) Clear authentication sessions on port" print "7) Erase port config"
# Get current time for later calculations on how long script took to run startTime = fn.getCurrentTime() # Make new directory for the current date fn.makeDirectory(outputDirectory) # Loop for each listed item imported into fileLines array for line in fileLines: # Strip newlines from imported devices line = fn.stripNewline(line) # Split each line on whitespace line = line.split(',') # Get running config from network device - line[1] is IP address commandRunConfig = sfn.runSSHCommand("show run", line[1], creds) # Save pulled running-config to file as a backup - line[0] is hostname backupFileName = "%s/%s_%s.txt" % (outputDirectory, line[0], currentTime) fn.writeCommandToFile(commandRunConfig, backupFileName) # Increment progress bar counter i += 1 # Progress bar for user on device count fn.printProgress(i, deviceCount, prefix = 'Progress:', suffix = 'Complete') # Print elapsed time for running script print "\nTotal elapsed time to complete script:" print fn.getScriptRunTime(startTime) print "\n"
# Find MAC address assigned to provided IP address macAddr = findMacbyIp(ipAddr, host) else: # This should never trigger fn.debugErrorOut('101') # Find what switch the given MAC address is on while True: # Set to run twice. If first MAC address lookup fails, script will ping the IP to force the MAC address to # populate in the MAC table, then rechecks MAC table for a in range(0,2): showMac = "show mac address-table | include %s" % (macAddr) # Run 1st command, save output to 'result' result = sfn.runSSHCommand(showMac, host, creds) # If outputList is empty, ping IP address from switch and recheck MAC address table (to force it to populate) if not result: commandPing = "ping " + ipAddr sfn.runSSHCommand(commandPing, host, creds) sleep(1) a += 1 else: # Break from 'for' loop break # Replace everywhere with multiple spaces with only a single space result1 = fn.replaceDoubleSpaces(result) # Split result by individual spaces outputList = result1.split(" ") # Set last index for outputList to 2nd from last if last index is empty