Skip to content

Instantly share code, notes, and snippets.

@avargas
Created March 7, 2013 16:53
Show Gist options
  • Save avargas/5109610 to your computer and use it in GitHub Desktop.
Save avargas/5109610 to your computer and use it in GitHub Desktop.
returns an array of start and end ip ranges in int
def cidr_range(cidr):
ip, subnet = cidr.split('/')
ipint = ip2int(ip)
subnet = int(subnet)
mask = ''.join(['1' for num in range(subnet)] + ['0' for num in range(32 - subnet)])
inverted_mask = ''.join(['0' for num in range(subnet)] + ['1' for num in range(32 - subnet)])
mask_int = int(mask, 2)
inverted_mask_int = int(inverted_mask, 2)
network = ipint & mask_int
return (network, network | inverted_mask_int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment