Automating IP subnets allocation using Python

Allocating IP subnets is a simple task for experienced Network Engineers. However, this simple task is prone to human error specially when hundreds of subnets are supposed to be allocated. Most common mistakes are allocating invalid and conflicting subnets. An example of an invalid subnet is This subnet is invalid because is not a network address for /27 mask. And an example of conflicting subnets would be and These are conflicting subnets because there are overlapping IPs between them. In this article, I’ll show you how to write a script in Python to build these checks automatically into your excel sheet. I’ll also show you how to create a script to automate the process of IP allocation altogether. For example, lets say you need to allocate subnets with following masks /28, /28, /27, /27, /29, /29 starting from network address, then the expected answer would be,,,,, and Without wasting more time, lets get started!

Solving the problem of invalid subnets

First we need to understand why a subnet can be invalid. we know that is a valid subnet, whereas, is not. To understand why, lets convert both subnets to binary form. in binary form can be written as 11000000101010000000000000010000. Next, we’ll enclose the network bits in square brackets [] and hosts bits in curly brackets {}. So, in binary can be written as [1100000010101000000000000001]{0000} and as [110000001010100000000000000]{10000}. By definition, binary numbers enclosed in [] bracket is a network address only if all bits inside {} bracket are zeros. Since, the former subnet ( has a bit set to one inside curly bracket {10000}, a not a correct network address for /27 mask. Lets see how we can apply this logic in Python, step by step

successfully verified the sanity of subnets
error occured. is incorrect subnet
error occured. is incorrect subnet

Solving the problem of conflicting subnets

Lets recap the problem we are trying to solve here. Consider the two subnets, and as an example. These two subnets cannot be allocated together because there are overlapping IPs between them. We can verify this by applying following steps. i) Select the smaller mask, in our case since 28 < 29, mask is equal to 28. ii) Convert both IPs into binary form, then enclose first 28 bits in square brackets. We get [1100000010101000000000000000]{0000}’ and ‘[1100000010101000000000000000]{1000} ’. Next, compare the terms inside square brackets, if the terms match, the subnets are conflicting otherwise they are fine. Python implementation for these step are given below

Example of non-conflicting list
No conflict detected
Example of conflicting list
('', '')

Automation Subnets allocation

When subnets are allocated manually, the issues discussed above arises very frequently. The best way to avoid these issues is to automate the process of allocation itself. In this section, we will discuss how this can be done. Lets recap the problem we are trying to solve briefly. Suppose say you were given a task to assign subnets with following masks /28, /28, /27, /27, /29, /29 starting from network address then we want the Python method to return,,,, and subnets. The steps we will be following to achieve desired results are as follows

['', '', '', '', '', '']