#!/usr/local/bin/python # This uses Googles ipaddr.py from http://code.google.com/p/ipaddr-py/ import ipaddr def cidr2tcprules(cidr): y = ipaddr.IPv4Network(cidr) start = str(y.network).split('.') end = str(y.broadcast).split('.') for i in range(4): if start[i] != end[i]: break if start[i] == '0' and end[i] == '255': lastbit = "" else: lastbit = "%s-%s." % ( start[i], end[i] ) firstbit = ".".join(start[:i]) return "%s.%s" % ( firstbit, lastbit ) if __name__ == '__main__': print cidr2tcprules('192.168.0.0/17')