Skip to content

Instantly share code, notes, and snippets.

@jayswan
Created December 21, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayswan/1508237 to your computer and use it in GitHub Desktop.
Save jayswan/1508237 to your computer and use it in GitHub Desktop.
Python Script to find duplicate Cisco interface configs
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"
print
for item in v:
print '\t' + item
print
print "are configured like"
print
for item in k:
print item
print
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
print_dup_info(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment