Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Last active September 1, 2022 00:43
Show Gist options
  • Select an option

  • Save cloudnull/ccd64a118c96ffd77d9c to your computer and use it in GitHub Desktop.

Select an option

Save cloudnull/ccd64a118c96ffd77d9c to your computer and use it in GitHub Desktop.
From within a predefined set of cidrs print a subnet from a requested section
import sys
import netaddr
# These are the predefined subnets which will print a given section. There
# are a total of 64 sections in either network.
# The constant is a list of tuples.
LAB_CIDRS = [
('tunnel', '172.29.236.0/22', 28),
('container', '10.240.0.0/16', 22)
]
def _get_cidr(index_num, cidr, section):
"""Return a subnet from within a CIDR.
:param index_num: ``int``
:param cidr: ``str``
:param section: ``int``
:returns: ``str``
"""
_cidr = netaddr.IPNetwork(cidr)
return [i for i in _cidr.subnet(section)][index_num]
def main(index_num):
"""Print the type and lab cidr from a user defined index.
:param index_num: ``int``
"""
if index_num > 63:
raise SystemExit(
'Number must be between 0 - 64.'
)
for name, cidr, section, in LAB_CIDRS:
print(
'Type: [ %s ] Subnet: [ %s ]' % (
name, _get_cidr(
index_num=index_num,
cidr=cidr,
section=section
)
)
)
if __name__ == "__main__":
main(index_num=int(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment