Wednesday, December 21, 2011

Find Unique Interface Configs on Switches

Recently I needed to find all of the interfaces with non-standard configurations on a bunch of Catalyst 3750 switches. I wrote a simple Python script to automate the process. To run this, save it in a directory with all of the configurations you want to check. Then edit the name.startswith() section near the bottom to match the naming convention for your config files; in my case all of my configs start with "c3750". Then run the script from your CLI:

>python dup_conf.py

If you're on a Mac or Linux system, Python should be installed by default. On Windows, you'll need to install it or run Cygwin. Note that in the default version, the script ignores SVIs and Gigabit interfaces; uncomment the appropriate lines to include them.

The output looks like this:


in config file c3750-1A.txt

interfaces

interface FastEthernet1/0/35

configured like

switchport trunk encapsulation dot1q
switchport trunk native vlan 76
switchport mode trunk
switchport voice vlan 65
no logging event link-status
srr-queue bandwidth share 10 10 60 20
srr-queue bandwidth shape 10 0 0 0
mls qos trust device cisco-phone
mls qos trust dscp
auto qos voip cisco-phone
no mdix auto
spanning-tree portfast

**************************************************
interfaces

interface FastEthernet1/0/11
interface FastEthernet1/0/12

configured like

switchport trunk encapsulation dot1q
switchport trunk native vlan 80
switchport mode trunk
switchport voice vlan 65
srr-queue bandwidth share 10 10 60 20
srr-queue bandwidth shape 10 0 0 0
mls qos trust device cisco-phone
mls qos trust dscp
auto qos voip cisco-phone
no mdix auto
spanning-tree portfast

**************************************************
interfaces

interface FastEthernet1/0/1
interface FastEthernet1/0/2
interface FastEthernet1/0/3
interface FastEthernet1/0/4
interface FastEthernet1/0/5
interface FastEthernet1/0/6
interface FastEthernet1/0/7
interface FastEthernet1/0/8
interface FastEthernet1/0/9
interface FastEthernet1/0/10
interface FastEthernet1/0/13
interface FastEthernet1/0/14
interface FastEthernet1/0/15
interface FastEthernet1/0/16

configured like

switchport access vlan 64
switchport voice vlan 65
srr-queue bandwidth share 10 10 60 20
srr-queue bandwidth shape 10 0 0 0
mls qos trust device cisco-phone
mls qos trust dscp
auto qos voip cisco-phone
no mdix auto
spanning-tree portfast

**************************************************


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)
view raw dup_conf.py hosted with ❤ by GitHub