#!/usr/bin/env python ## ## netblock.py ## ## Author: Michal Ludvig ## http://www.logix.cz/michal ## ## Simple tool to compute an IPv4 netblock covering ## all IP addresses given on the command line. ## ## For example: ## ~$ netblock 1.2.3.4 1.2.3.255 ## 1.2.3.0/24 ## ## ~$ netblock 192.168.131.12 192.168.137.1 192.168.144.9 ## 192.168.128.0/19 ## import sys import socket import struct # functions dottedQuadToNum(), numToDottedQuad() and makeMask() taken # from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66517 # and made endian-agnostic def dottedQuadToNum(ip): "convert decimal dotted quad string to long integer" return struct.unpack('>L',socket.inet_aton(ip))[0] def numToDottedQuad(n): "convert long int to dotted quad string" return socket.inet_ntoa(struct.pack('>L',n)) def makeMask(n): "return a mask of n bits as a long integer" return ~((2L< ... " sys.exit(1)