Skip to content

Instantly share code, notes, and snippets.

@ovidiucs
Last active August 29, 2015 14:21
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 ovidiucs/5e53fcba72018e1ed5ef to your computer and use it in GitHub Desktop.
Save ovidiucs/5e53fcba72018e1ed5ef to your computer and use it in GitHub Desktop.
Subnet for loop for IPv4
units = [1 << (8*i) for i in range(3,-1,-1)]
def ip_to_int(ip):
return sum(int(byte) * unit for(byte,unit) in zip(ip.split('.'), units))
def int_to_ip(i):
return '.'.join(str((i/bit) & 0xff) for bit in units)
start_ip = '1.0.0.0'
end_ip = '1.0.0.255'
ips = (int_to_ip(i) for i in xrange(ip_to_int(start_ip),ip_to_int(end_ip)))
for ip in ips:
print str(ip).split('.')
# http://stackoverflow.com/questions/23459319/how-to-loop-through-an-ip-range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment