#!/usr/bin/python -O # # Dinko Korunic 'kreator', 2005. # generate-ips.py # script for generating IPs from given range # # Copyright (C) 2005 Dinko Korunic, InfoMAR d.o.o. # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # $Id: generate-ips.py 162 2005-07-19 22:25:21Z kreator $ import IPy, sys, getopt def usage(): usage = """$Id: generate-ips.py 162 2005-07-19 22:25:21Z kreator $ Usage: %s [-h|--help] [-f|--file filename ...] target [...] Summary: This program does calculates IP addresses from given set of CIDRs, ranges, masks or classes, with possible exceptions stored in a file.""" print usage % sys.argv[0] sys.exit(1) def readblacklist(filename): blacklist = {} try: exceptions = file(filename, 'r') except IOError: return blacklist for line in exceptions: myrange = IPy.IP(line) for ip in myrange: blacklist[ip] = 1 exceptions.close() return blacklist def main(argv): try: opts, args = getopt.gnu_getopt(argv, 'hf:', ['help', 'file=']) except getopt.GetoptError: usage() blacklist = {} # ugly argument parse for opt, optarg in opts: if opt in ('-h', '--help'): usage() elif opt in ('-f', '--file'): blacklist = readblacklist(optarg) else: usage() if len(args) < 1: usage() for myinput in args: myrange = IPy.IP(myinput) for ip in myrange: if not blacklist.has_key(ip): print ip ###### main body ###### if __name__ == '__main__': main(sys.argv[1:])