alanwill alanwill - 3 months ago 31
Python Question

Splitting a network into subnets of multiple prefixes

I'm using the

netaddr
module and trying to figure out how/if I can split a network into subnets of varying prefixes. For example take a /16 and split it into X /23s and Y /24s.

From what I can tell we can use the
subnet
function to split a network in X number of a given prefix but it only takes 1 prefix.

The above would slice out 4 /23s out of the /16 which is great, but how do I take the remaining space and slice that into a differing prefix?

ip = IPNetwork('172.24.0.0/16')
subnets = list(ip.subnet(23, count=4))


Is there a way that I can achieve what I'm trying to do with netaddr?

Answer

Try the below

from netaddr import IPNetwork, cidr_merge, cidr_exclude

class IPSplitter(object):
    def __init__(self, base_range):
        self.avail_ranges = set((IPNetwork(base_range),))

    def get_subnet(self, prefix, count=None):
        for ip_network in self.get_available_ranges():
            subnets = list(ip_network.subnet(prefix, count=count))
            if not subnets:
                continue
            self.remove_avail_range(ip_network)
            self.avail_ranges = self.avail_ranges.union(set(cidr_exclude(ip_network, cidr_merge(subnets)[0])))
            return subnets

    def get_available_ranges(self):
        return sorted(self.avail_ranges, key=lambda x: x.prefixlen, reverse=True)

    def remove_avail_range(self, ip_network):
        self.avail_ranges.remove(ip_network)

You can use it like below. With some more math you can make it more efficient,like chopping of subnets from two different blocks when count is not satisfied from a single block.

In [1]: from ip_splitter import IPSplitter

In [2]: s = IPSplitter('172.24.0.0/16')

In [3]: s.get_available_ranges()
Out[3]: [IPNetwork('172.24.0.0/16')]

In [4]: s.get_subnet(23, count=4)
Out[4]: 
[IPNetwork('172.24.0.0/23'),
 IPNetwork('172.24.2.0/23'),
 IPNetwork('172.24.4.0/23'),
 IPNetwork('172.24.6.0/23')]

In [5]: s.get_available_ranges()
Out[5]: 
[IPNetwork('172.24.8.0/21'),
 IPNetwork('172.24.16.0/20'),
 IPNetwork('172.24.32.0/19'),
 IPNetwork('172.24.64.0/18'),
 IPNetwork('172.24.128.0/17')]

In [6]: s.get_subnet(28, count=10)
Out[6]: 
[IPNetwork('172.24.8.0/28'),
 IPNetwork('172.24.8.16/28'),
 IPNetwork('172.24.8.32/28'),
 IPNetwork('172.24.8.48/28'),
 IPNetwork('172.24.8.64/28'),
 IPNetwork('172.24.8.80/28'),
 IPNetwork('172.24.8.96/28'),
 IPNetwork('172.24.8.112/28'),
 IPNetwork('172.24.8.128/28'),
 IPNetwork('172.24.8.144/28')]

In [7]: s.get_available_ranges()
Out[7]: 
[IPNetwork('172.24.8.128/25'),
 IPNetwork('172.24.9.0/24'),
 IPNetwork('172.24.10.0/23'),
 IPNetwork('172.24.12.0/22'),
 IPNetwork('172.24.16.0/20'),
 IPNetwork('172.24.32.0/19'),
 IPNetwork('172.24.64.0/18'),
 IPNetwork('172.24.128.0/17')]