Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Last active January 16, 2023 07:16
Show Gist options
  • Select an option

  • Save devinci-it/bbe5494132d54769f39a55bccf3a265b to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/bbe5494132d54769f39a55bccf3a265b to your computer and use it in GitHub Desktop.
Subnetting and VLSM/
from ipaddress import IPv4Network
from math import log2
#TO_SUBNET = IPv4Network("10.0.0.0/8")
#NEW_PREFIX=[24,24,24]
#COUNT=8
def vlsm(TO_SUBNET,NEW_PREFIX):
CUR_NETWORKS = []
NEW_PREFIX.sort()
subnet_list=TO_SUBNET.subnets(new_prefix=NEW_PREFIX[0])
CUR_NETWORKS.append(subnet_list.__next__())
NEW_PREFIX.pop(0)
for each_netmask in NEW_PREFIX:
next_subnet = subnet_list.__next__()
to_add=next_subnet.subnets(new_prefix=each_netmask)
CUR_NETWORKS.append(to_add.__next__())
return CUR_NETWORKS
def subnet(to_subnet,subnet_count):
to_subnet:IPv4Network
pref_diff = log2(subnet_count).__ceil__()
nextworks = to_subnet.subnets(prefixlen_diff=pref_diff)
return [net for net in nextworks]
#test1=(subnet(TO_SUBNET,COUNT))
#test2=vlsm(TO_SUBNET,NEW_PREFIX)
#print(text1,test2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment