Created
December 21, 2011 23:44
-
-
Save jayswan/1508237 to your computer and use it in GitHub Desktop.
Python Script to find duplicate Cisco interface configs
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
import os | |
def print_dup_info(s): | |
#split config on ! characters | |
blocks = s.split('!') | |
stanza_list = [] | |
interface_dict = {} | |
for block in blocks: | |
#get rid of blank lines and split each stanza into a list of lines | |
if block == '\n': | |
continue | |
stanza_lines = block.split('\n') | |
stanza_list.append(stanza_lines) | |
for stanza in stanza_list: | |
#remove empty items & descriptions | |
stanza[:] = [item for item in stanza if item != ''] | |
stanza[:] = [item for item in stanza if not item.startswith(' description')] | |
#push config into a dictionary with the interface config tuple as key | |
#the value is a list of interface names; this way duplicate interface configs | |
#get a list of interface names with duplicate configs | |
# comment out next two lines to include SVIs | |
if stanza[0].startswith('interface Vlan'): | |
continue | |
# comment out next two lines to include Gigabit interfaces | |
if stanza[0].startswith('interface Giga'): | |
continue | |
if stanza[0].startswith('interface '): | |
try: | |
# append to dict value if it's already there | |
interface_dict[tuple(stanza[1:])].append(stanza[0]) | |
except KeyError: | |
interface_dict[tuple(stanza[1:])] = [] | |
interface_dict[tuple(stanza[1:])].append(stanza[0]) | |
for k,v in interface_dict.items(): | |
print "interfaces" | |
for item in v: | |
print '\t' + item | |
print "are configured like" | |
for item in k: | |
print item | |
print '*'*50 | |
filenames = [name for name in os.listdir('.') if name.startswith('c3750')] | |
for file in filenames: | |
f = open(file) | |
s = f.read() | |
f.close() | |
print "in config file " + file | |
print_dup_info(s) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment