Skip to content

Instantly share code, notes, and snippets.

@mdfranz
Created August 3, 2013 23:03
Show Gist options
  • Save mdfranz/6148318 to your computer and use it in GitHub Desktop.
Save mdfranz/6148318 to your computer and use it in GitHub Desktop.
Getting the first two /24's of a /16 (well sort of) using Python netaddr
In [1]: import netaddr
In [2]: vpc_block = netaddr.IPNetwork("192.168.0.0/16")
In [3]: vpc_subnets = list(vpc_block.subnet(24))
In [4]: vpc_subnets[1:3]
Out[4]: [IPNetwork('192.168.1.0/24'), IPNetwork('192.168.2.0/24')]
In [5]: str(vpc_subnets[2])
Out[5]: '192.168.2.0/24'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment