Last active
August 29, 2015 14:21
-
-
Save dovideh/5e53fcba72018e1ed5ef to your computer and use it in GitHub Desktop.
Subnet for loop for IPv4
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
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