Skip to content

Instantly share code, notes, and snippets.

@stestagg
Created February 18, 2013 15:37
Show Gist options
  • Save stestagg/4978271 to your computer and use it in GitHub Desktop.
Save stestagg/4978271 to your computer and use it in GitHub Desktop.
Iterate over all IP addresses in a range..
def get_addr_range(start, end):
def to_int(ip):
return sum([int(p) << (24 - (8 * e)) for e, p in enumerate(ip.split("."))])
def to_ip(val):
return ".".join(str((val & (0xff << s)) >> s) for s in range(24, -1, -8))
for addr in xrange(to_int(start), to_int(end) + 1):
yield to_ip(addr)
for x in get_addr_range('0.0.0.0', '224.4.1.254'):
print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment