#!/usr/bin/env python import itertools import ipv4 def overlap(a, b): if b.network.numeric_address < a.network.numeric_address: a, b = b, a return a.network.numeric_address <= b.broadcast.numeric_address and b.network.numeric_address <= a.broadcast.numeric_address def readNets(filenames): for line in itertools.chain(*map(file, filenames)): parts = line.strip().split(None, 1) net = ipv4.CIDR(parts[0]) info = parts[1] if len(parts) == 2 else None yield (net, info) def getMatches(nets, targets): for (net, info) in nets: if any([overlap(net, target) for target in targets]): yield (net, info) def main(filenames, targets): if not targets: return "no target specified" else: targetnets = map(ipv4.CIDR, targets) matches = 0 for (net, info) in getMatches(readNets(filenames), targetnets): print "%s\n\t%s\n" % (net, info) matches += 1 print "%d match%s for %s" % (matches, ('es' if matches != 1 else ''), ', '.join(map(str, targetnets))) if __name__ == '__main__': import sys from os import path from glob import iglob filenames = iglob(path.join(path.dirname(path.realpath(__file__)), '*.net')) sys.exit(main(filenames, sys.argv[1:]))