#!/bin/env python import re from argparse import ArgumentParser def expandrange(rangefunc,stepcnt=['1']): hosts = [] step = str(stepcnt[0]) octets = rangefunc.split('.') for i,octet in enumerate(octets): if '-' in octet: octetrange = octet.split('-') for digit in range(int(octetrange[0]), int(octetrange[1])+1, int(step)): ip = '.'.join(octets[:i] + [str(digit)] + octets[i+1:]) hosts += expandrange(ip,stepcnt) break else: hosts.append(rangefunc) return hosts parser = ArgumentParser('pingrange') parser.add_argument('ip', help='IP range to ping, e.g., 10.1.0-1.0-255 will expand to 10.1.0.0/23') parser.add_argument('options', type=int, default=['1'], nargs='*', help='Options to pass to ping') args = parser.parse_args() targets = expandrange(args.ip,args.options) for ip in targets: print 'IP : %s' % ip