Created
September 13, 2021 21:42
-
-
Save liamkeegan/c967688177f953d88442c4182898bd06 to your computer and use it in GitHub Desktop.
NXOS ARP to IP Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Quick and dirty MAC address to IP address lookup tool. Creates CSV compliant data. A total hack done in 15 minutes, saving HOURS of work. | |
In the mac_table.txt, have a list of mac addresses from 'show mac address-table'. Remove the headers. | |
192.168.148.31 00:08:17 0050.56b9.dddd Vlan128 | |
192.168.148.146 00:01:45 8c60.4f54.eeee Vlan128 | |
192.168.144.32 00:17:08 0050.56b9.ffff Vlan134 | |
192.168.144.21 00:00:41 0050.56bb.gggg Vlan104 | |
In your port_sw.txt file, grab the ARP entries from 'show ip arp'. Remove the headers. | |
* 379 0014.4ff9.2ccc dynamic 0 F F Eth115/1/20 | |
* 442 4cd9.8f64.8aaa dynamic 0 F F Eth113/1/19 | |
* 442 4cd9.8f64.9122 dynamic 200 F F Eth115/1/11 | |
When you run the program, it outputs a CSV that you can throw into Excel to create a cut sheet. Includes port, mac, VLAN and IP. | |
Eth125/1/4,0014.4ffb.ddd2,128,192.168.128.12 | |
Eth135/1/6,5cf3.fcea.ccc2,128,192.168.128.95 | |
Eth101/1/42,a4bf.0137.bbbc,128,192.168.128.229 | |
Eth165/1/12,b026.28e5.abcd,128,192.168.128.59 | |
''' | |
# Open the MAC list and parse it first | |
with open("mac_table.txt") as mac_file: | |
mac_entries = mac_file.readlines() | |
# Create a temp dictionary for the contents of the MAC table. | |
mac_dict = {} | |
# Iterate through each line, reading it into the table. Set the mac_dict key to temp[2] (the MAC) and temp[0] to the IP address. | |
for line in mac_entries: | |
temp = str.split(line) | |
mac_dict[temp[2]] = temp[0] | |
###################### | |
# Open the port list and parse it first | |
with open('port_sw.txt') as port_file: | |
port_entries = port_file.readlines() | |
# Iterate through each port | |
for port in port_entries: | |
temp = str.split(port) | |
# Lookup MAC address in the mac_dict dict - this goes in a try/except block because there are certain ports that don't have IPs. If that happens, just put a '-' in the entry | |
try: | |
ip_addr = mac_dict[temp[2]] | |
except: | |
ip_addr = '-' | |
# Physical port, MAC address, VLAN, IP address (or a - if one doesn't exist). Prints this out to the console. | |
print(f"{temp[7]},{temp[2]},{temp[1]},{ip_addr}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment