## Stuff for streamlining ## Split out a "category" by what the header would contain. def splitInterfaces(filename, searchterms, ignorelist = []): ## TODO ### ## 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: # Skip a sub-information ending interfaceDic.append(" !") continue if '!' in line and bit != 0: # Comment or end of interface # input(interfaceDic) if " shutdown\n" in interfaceDic and "no shut" not 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) ## see if we found the start of a new section we want 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 ## Add fix for bgp et all if "bgp" in line.split(" "): bit = str(line.split(" ")[2]) ## Sanity check for ignore in ignorelist: # each of the terms we are told to ignore if ignore in line: # if a ignore term is in our line bit = 0 continue # dont traverse this return deviceDic def splitsubInterfaces(subDict, ignorelist = []): # menus within menus attr = [] # this is going to be our sub lines for each of our parent interfaces for x in subDict: ## Add the list from dict to db attr = subDict[x] ## Now split into 3 or whatever dicts subs = {}; bit = 'global' # Oustide of address family etcs subs[bit] = [attr[0]] for line in attr: if "!" in line: bit = 0 continue if bit == 0: bit = line.replace("\n", "") if bit != 0 and bit in subs: subs[bit].append(line.replace("\n", "")) if bit != 0 and bit not in subs: subs[bit] = [] return subs 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 searchMultiple(item, attributeList, ignore=[]): # find multiple instances of a word, eg. network statements found = []; bit = False for x in attributeList: if item in x: for i in ignore: if i in ignore: continue bit = True # Trigger the found 'bit' found.append(x) if bit: return found else: 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) 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 ## 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, ['interface GigabitEthernet', 'interface Loopback','interface Port-channel'], ['ssh', 'source-interface', ]) 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 ", "") if '- - - - - -' in desc and '-' in ipv4[2]: continue else: output.write(",{},{},{},{}, ,{}\n".format(port.replace("\n", ""),ipv4[3],ipv4[4],ipv6,desc)) except: pass output.close() def generateBGPcsv(filenamesList): output = open("BGPinfo.csv", "w+") searchterms = ["router bgp"] ignorelist = ['exec'] for filename in filenamesList: BGPattr = splitInterfaces(filename, searchterms, ignorelist) BGPattrS = splitsubInterfaces(BGPattr, ['']) for x in BGPattrS: ASNUM = (BGPattrS[x][1].lstrip().split(" ")[2]) break output.write("\n\nBGP INFO FOR: {},AS: {}\n\n".format(filename.replace(".txt", ""), ASNUM)) # Write the info out to csv # input(BGPattrS) for ASnum in BGPattrS: ## Record Neighbours neighbors = searchMultiple('neighbor', BGPattrS[ASnum]) advernet = searchMultiple('network', BGPattrS[ASnum]) remoteNei = [] passNei = [] source = [] ### Catch anything and sort where we need it for x in neighbors: # print(x) if 'remote-as' in x or 'activate' in x: remoteNei.append(x) if 'password' in x: passNei.append(x) ## TODO - maybe remove all that found a home then # add a coloum with extra attributes output.write("{}".format(ASnum.lstrip().upper())) # Add remote ASes to csv if advernet != []: output.write("\n,Advertised Nets:\n") for x in advernet: if 'description' in advernet: continue properties = x.lstrip().split(" ") if len(properties) < 3: # IPV6 output.write(",{}\n".format(properties[1])) else: output.write(",{},{}\n".format(properties[1],properties[3])) output.write("\n") if remoteNei != []: output.write("\n,Neighbours:\n") for x in remoteNei: properties = x.lstrip().split(" ") if len(properties) < 4: output.write(",{},ACTIVATED\n".format(properties[1])) else: output.write(",{},{}\n".format(properties[1],properties[3])) output.write("\n") if passNei != []: output.write("\n,Neighbours w/ auth:\n") for x in passNei: properties = x.lstrip().split(" ") output.write(",{},ENABLED\n".format(properties[1])) output.write("\n") output.close() def generateOSPFcsv(filenamesList): output = open("OSPFinfo.csv", "w+") searchterms = ["router ospf"] ignorelist = ['exec'] for filename in filenamesList: ospf = splitInterfaces(filename, searchterms, ignorelist) try: routerid = ospf['ospf'][0].lstrip().split(" ")[1].replace("\n", "") except: continue output.write("\n\nOSPF INFO FOR: {},Router-ID: {}\n".format(filename.replace(".txt", ""),routerid)) output.write("\n,Network Redistribute:\n") ## network = searchMultiple('network', ospf['ospf']) for statement in network: statement = statement.lstrip().replace("\n", "").split(" ") output.write(",{},{},{}\n".format(statement[1],statement[2],statement[4])) output.write("\n") output.write("\n,Active Interfaces:\n") ## active = searchMultiple('no passive-interface', ospf['ospf']) for interface in active: interface = interface.lstrip().replace("\n", "").split(" ") output.write(",{}\n".format(interface[2])) 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'] AllDevices = RouterConfigs + SwitchConfigs # MasterList ## Output Addressing Table generateAddressCSV(AllDevices) ## Output Vlan Table generateVlanCSV(SwitchConfigs) ## Output BGP Table generateBGPcsv(RouterConfigs) ## Output OSPF Table generateOSPFcsv(AllDevices) ## Output EIGRP Table # Seems kinda just basicy