Skip to content

Instantly share code, notes, and snippets.

@CaledoniaProject
Last active January 10, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaledoniaProject/5c9e1bcf1fb0a6aefafa45dddf7e4dd2 to your computer and use it in GitHub Desktop.
Save CaledoniaProject/5c9e1bcf1fb0a6aefafa45dddf7e4dd2 to your computer and use it in GitHub Desktop.
Expand IP range with Python, similar to nmap syntax
import itertools
def expand_ip_range(ipstr):
parts = ipstr.split('.')
param = []
for part in parts:
if '-' in part:
tmp = part.split('-')
start = 0
stop = 255
try:
start = int(tmp[0])
if start < 0 or start > 255:
start = 0
except:
pass
try:
stop = int(tmp[1])
if stop < 0 or stop > 255:
stop = 255
except:
pass
tmp = []
for i in range(start, stop + 1):
tmp.append(i)
param.append(tmp)
else:
param.append([part])
for row in itertools.product(*param):
yield '.'.join(map(str, row))
for row in expand_ip_range('192.168.154-155.10-11'):
print(row)
for row in expand_ip_range('192.168.250-.10-11'):
print(row)
for row in expand_ip_range('192.168.-2.10-11'):
print(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment