Skip to content

Instantly share code, notes, and snippets.

@scottyob
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.network.prefixlen, secondary) )
networks.append( (interface.network.prefixlen, 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(interface_address.network.network_address.compressed)
if not int_network_address in tree:
#This network does not collide with anything. Add it into the table
tree[str(interface_address.network.compressed)] = interface_address
else:
#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!
collides[tree[int_network_address]].append(interface_address)
for parent, children in collides.iteritems():
#Skip any /30's with two items in them
if parent.network.prefixlen == 30:
if len(children) == 1:
continue
elif parent.network.prefixlen < 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):
addresses.append(child.address)
else:
final_children.append(child)
children = final_children
if len(children) > 0:
print "'%s %s (%s)' collides with: " % (parent.parent_device.getHostname(), parent.name, parent.network.compressed )
for child in children:
print " %s %s (%s)" % (child.parent_device.getHostname(), child.name, child.network.compressed )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment