Skip to content

Instantly share code, notes, and snippets.

@sapamja
Created September 2, 2014 07:01
Show Gist options
  • Save sapamja/96c056c397125832bcb0 to your computer and use it in GitHub Desktop.
Save sapamja/96c056c397125832bcb0 to your computer and use it in GitHub Desktop.
ip_sub.py
import socket, struct
def ip_to_decimal(ip):
return struct.unpack("!L", socket.inet_aton(ip))[0]
def decimal_to_ip(decimal):
return socket.inet_ntoa(struct.pack("!L", decimal))
cidr = '192.168.0.1/24'
network, netmask = cidr.split('/')
decimal = ip_to_decimal(network)
last_valid = 1 << 32 - int(netmask)
for i in range(last_valid - 1):
print decimal_to_ip(decimal + i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment