Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2014 06:04
Show Gist options
  • Save scottyob/9182695 to your computer and use it in GitHub Desktop.
Save scottyob/9182695 to your computer and use it in GitHub Desktop.
Prints out a list of overlapping/duplicate IP addresses
import os
import sys
import cPickle as pickle
import heapq
import pprint
from SubnetTree import SubnetTree
networks = []
# Read from our pickled files. Generate a list of networks sorted by the CIDR
for filename in os.listdir('/configs/pickle/'):
device = pickle.load( open('/configs/pickle/%s' % filename) )
for interface in device.interfaces:
for secondary in interface.secondary:
networks.append( (, secondary) )
networks.append( (, interface) )
networks = sorted(networks)
#Setup the initial collisions.. does not yet keep track of devices in networks.
tree = SubnetTree()
collides = {}
for item in networks:
# from pudb import set_trace; set_trace()
interface_address = item[1]
int_network_address = str(
if not int_network_address in tree:
#This network does not collide with anything. Add it into the table
tree[str(] = interface_address
#This network collides with something already in the tree
if not collides.has_key(tree[int_network_address]):
collides[tree[int_network_address]] = []
#Add the offending collider!
for parent, children in collides.iteritems():
#Skip any /30's with two items in them
if == 30:
if len(children) == 1:
elif < 30:
# For networks that are /24's. Remove any any children that does not have clashing IP addresses
addresses = []
#The final list of children we want to return. Don't add items that have the same netmask and non-overlapping IP addresses
# Just assume they're all in the same L2 domain
final_children = []
for child in children:
if (child.netmask == parent.netmask) and (not child.address in addresses):
children = final_children
if len(children) > 0:
print "'%s %s (%s)' collides with: " % (parent.parent_device.getHostname(),, )
for child in children:
print " %s %s (%s)" % (child.parent_device.getHostname(),, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment