Last active
December 25, 2015 02:59
-
-
Save miku/6906209 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Turn things like 10.[3-25].0.X into list of IPs. | |
""" | |
import re | |
import sys | |
import itertools | |
def octet(s): | |
""" | |
Takes a string which represents a single octet. | |
Returns a list of values. | |
""" | |
if s == 'X': | |
return xrange(256) | |
try: | |
low, high = [int(val) for val in s.strip('[]').split('-')] | |
if low > high or low < 0 or high > 255: | |
raise RuntimeError('That is no valid range.') | |
return xrange(low, high + 1) | |
except ValueError as err: | |
number = int(s) | |
if not 0 <= number <= 255: | |
raise ValueError('Only 0-255 allowed.') | |
return [number] | |
if __name__ == '__main__': | |
try: | |
template = sys.argv[1] | |
octets = [octet(s) for s in template.split('.')] | |
# print(octets) | |
for parts in itertools.product(*octets): | |
print('.'.join(map(str, parts))) | |
except IndexError as err: | |
print('Usage: %s IP-TEMPLATE' % (sys.argv[0])) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment