126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
## Stuff for streamlining
|
|
|
|
def splitInterfaces(filename, searchterms = ['interface GigabitEthernet', 'interface Loopback','interface Port-channel', 'interface Vlan']):
|
|
##
|
|
deviceDic = {} # All interfaces
|
|
interfaceDic = [] # Single interface
|
|
##
|
|
device = open(filename, 'r')
|
|
bit = 0
|
|
for line in device: # Read each line
|
|
if len(line.split(" ")) != 2 and bit == 0: # If another type of Gigabit ethernet config
|
|
continue # ignore
|
|
## Actual Logic
|
|
if '!' in line and bit != 0: # Comment or end of interface
|
|
# input(interfaceDic)
|
|
if " shutdown\n" in interfaceDic:
|
|
# input(bit)
|
|
interfaceDic = 'shutdown'
|
|
deviceDic[bit] = interfaceDic # push to the main dict
|
|
interfaceDic = [] # Clear the interfaces
|
|
bit = 0
|
|
continue
|
|
if bit != 0: # If we are traversing an interface record the info
|
|
interfaceDic.append(line)
|
|
for term in searchterms: # each of the terms we are looking for in 'searchterms' list
|
|
if term in line: # If we find a term line
|
|
bit = str(line.split(" ")[1]) # Set the traversing bit to the name
|
|
return deviceDic
|
|
|
|
def searchList(item, attributeList):
|
|
for x in attributeList:
|
|
if 'link-local' in x:
|
|
continue
|
|
if 'no' in x:
|
|
continue
|
|
if item in x:
|
|
return x
|
|
return '- - - - - - description \n'
|
|
|
|
|
|
def generateVlanCSV(filenamesList): # Records the VLAN SVIs to a file
|
|
output = open("Vlans.csv", "w+")
|
|
for filename in filenamesList:
|
|
searchterms = ['interface Vlan', 'vlan ']
|
|
vlans = splitInterfaces(filename, searchterms)
|
|
|
|
# for x in vlans:
|
|
# print('*********')
|
|
# print(x)
|
|
# print('--')
|
|
# print(vlans[x])
|
|
# print('*********')
|
|
|
|
output.write(filename.replace(".txt", "\n"))
|
|
|
|
# Write the ports out to csv
|
|
for port in vlans:
|
|
if vlans[port] == 'shutdown': # If port is shutdown
|
|
# input("Here")
|
|
output.write(",{},SHUTDOWN\n".format(port.replace("\n", "")))
|
|
continue
|
|
if 'Vlan' not in port: # Only Deal with the SVIs right now
|
|
continue
|
|
|
|
# Other catchs
|
|
|
|
# Vlan99
|
|
# [' ip address 10.1.99.252 255.255.255.0\n', ' no ip proxy-arp\n', ' standby 99 ip 10.1.99.254\n', ' standby
|
|
# 99 priority 110\n', ' standby 99 preempt\n', ' standby 99 track 23 decrement 20\n', ' ipv6 address FE80::D1
|
|
# link-local\n', ' ipv6 address 2001:DB8:CAFE:99::D1/64\n', ' no shutdown\n']
|
|
|
|
# Write normal VLAN output
|
|
try:
|
|
ipv4 = searchList("ip address", vlans[port]).replace("\n", "").split(" ")
|
|
ipv6 = searchList("ipv6 address", vlans[port]).split(" ")[3].replace("\n", "")
|
|
hsrpType = searchList("priority", vlans[port]).split(" ")
|
|
if 'priority' in hsrpType:
|
|
priority = 'ACTIVE'
|
|
else:
|
|
hsrpType = ['','','','','100']
|
|
priority = 'STANDBY'
|
|
tracked = searchList("track", vlans[port]).split(" ")
|
|
if 'track' in tracked:
|
|
tracked = "YES"
|
|
else:
|
|
tracked = "NO"
|
|
output.write(",{},{},{},{}, ,{},{},{}\n".format(port.replace("\n", ""),ipv4[3],ipv4[4],ipv6,hsrpType[4].replace("\n",""),priority,tracked))
|
|
except:
|
|
pass
|
|
|
|
output.close()
|
|
|
|
def generateAddressCSV(filenamesList):
|
|
output = open("Addressing.csv", "w+")
|
|
for filename in filenamesList:
|
|
interfaces = splitInterfaces(filename)
|
|
output.write(filename.replace(".txt", "\n"))
|
|
|
|
# Write the ports out to csv
|
|
for port in interfaces:
|
|
if interfaces[port] == 'shutdown':
|
|
# input("Here")
|
|
output.write(",{},SHUTDOWN\n\n".format(port.replace("\n", "")))
|
|
continue
|
|
|
|
try:
|
|
ipv4 = searchList("ip address", interfaces[port]).replace("\n", "").split(" ")
|
|
ipv6 = searchList("ipv6 address", interfaces[port]).split(" ")[3].replace("\n", "")
|
|
desc = searchList("description", interfaces[port]).replace(" description ", "")
|
|
output.write(",{},{},{},{}, ,{}\n".format(port.replace("\n", ""),ipv4[3],ipv4[4],ipv6,desc))
|
|
except:
|
|
pass
|
|
|
|
output.close()
|
|
## Location of the configs
|
|
# Sorted by device type just incase
|
|
RouterConfigs = ['configs/R1.txt','configs/R2.txt','configs/R3.txt']
|
|
SwitchConfigs = ['configs/DLS1.txt', 'configs/DLS2.txt', 'configs/ALS1.txt']
|
|
# MasterList
|
|
AllDevices = RouterConfigs + SwitchConfigs
|
|
|
|
# Output Addressing Table
|
|
# generateAddressCSV(AllDevices)
|
|
|
|
# Output Vlan Table
|
|
generateVlanCSV(SwitchConfigs) |