Skip to content

Instantly share code, notes, and snippets.

@vicradon
Created May 25, 2024 20:05
Show Gist options
  • Save vicradon/1f139160c9d57a138109503bc19a7cc0 to your computer and use it in GitHub Desktop.
Save vicradon/1f139160c9d57a138109503bc19a7cc0 to your computer and use it in GitHub Desktop.
This gist contains a python script that outputs IP addresses and assigned interfaces from a file generated by the command 'show ip interface brief'
def get_file_content(filelocation):
try:
with open(filelocation, "r") as file:
content = file.read()
return content
except FileNotFoundError:
print("The file was not found.")
except PermissionError:
print("Permission denied.")
except Exception as e:
print(f"An error occurred: {e}")
def print_ip_and_interfaces(filecontent):
if not filecontent:
print("No file content was supplied")
return
lines = filecontent.split("\n")
ip_addresses = ["IP-Addresses"]
assigned_interfaces = ["Interfaces"]
for line in lines:
sections = line.split()
if sections and sections[1][0:3] == "10.":
ip_addresses.append(sections[1])
assigned_interfaces.append(sections[0])
ip_addresses_output = "\n".join(ip_addresses)
assigned_interfaces_output = "\n".join(assigned_interfaces)
print(ip_addresses_output)
print("\n")
print(assigned_interfaces_output)
if __name__ == "__main__":
'''
This script expects a file location inputed as a variable
It outputs the ip addresses and interfaces that those addresses are assigned to
'''
filelocation = "./show_ip_int_brief.txt"
filecontent = get_file_content(filelocation)
print_ip_and_interfaces(filecontent)
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0/0 10.220.88.22 YES NVRAM up up
GigabitEthernet0/0/1 unassigned YES unset administratively down down
GigabitEthernet0/1/0 unassigned YES unset down down
GigabitEthernet0/1/1 unassigned YES unset down down
GigabitEthernet0/1/2 unassigned YES unset down down
GigabitEthernet0/1/3 unassigned YES unset down down
Loopback98 10.98.98.1 YES manual up up
Loopback99 10.99.99.1 YES manual up up
Vlan1 unassigned YES manual up down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment