Post Image

Python ipaddress Module Get Next Network

When working with IP addresses and IP networks using pythons ipaddress module it is not straightforward how to get the next ipv4 subnet if you only have an IPv4Network object to go off of. In this post I will show you how to get the next IPv4Network object if you only have a single IPv4Network object to go off of.

 

Working out the Logic

Lets say that we start with the IPv4Network object of 10.100.0.0/20.

ipaddress.IPv4Network('10.100.0.0/20')

The 10.100.0.0/20 network encompasses addresses 10.100.0.0 - 10.100.15.255 so the next /20 network would be 10.100.16.0/20 so we need to find a way to get that next object.

 

Unfortunately, the IPv4Network object does not have an integrated way to give you the next network, however it does give us attributes that we can use to deduce the next /20 network. The specific piece that will be key is the broadcast address and the prefix length (cidr notation), below I have a Python console output showing how to access those attributes.

>>> starting_network = ipaddress.IPv4Network('10.100.0.0/20')
>>> starting_network.broadcast_address
IPv4Address('10.100.15.255')
>>> starting_network.prefixlen
20
>>> 

So we know the next IP address (10.100.16.0) is the network address of the following /20 network that we want to have. Thankfully the IPv4Address object allows you to do mathematical operations against it and it will return the correct IP address.

Knowing that all we need to do is add 1 to the broadcast address to get the proper address, Python console example below.

>>> starting_network.broadcast_address + 1
IPv4Address('10.100.16.0')

 

Now that we have the proper address object all we need to do is use that data and create a new IPv4Network object and we have the next available network.

>>> tgt_network_addr = starting_network.broadcast_address + 1
>>> next_network = ipaddress.IPv4Network(f"{tgt_network_addr}/{starting_network.prefixlen}")
>>> next_network
IPv4Network('10.100.16.0/20')
>>> 

 

Building a Proof of Concept Script

Now to make life a bit easier we can create a helper function that will ingest an IPv4Network object, and return an IPv4Network object of the next available IPv4 network with the same cidr.

def next_network(ipv4_subnet):
    tgt_network_addr = ipv4_subnet.broadcast_address + 1
    return ipaddress.IPv4Network(f"{tgt_network_addr}/{ipv4_subnet.prefixlen}")

 

And lastly making a small proof of concept script that uses our function above that also can be copied and run.

import ipaddress


def next_network(ipv4_subnet):
    tgt_network_addr = ipv4_subnet.broadcast_address + 1
    return ipaddress.IPv4Network(f"{tgt_network_addr}/{ipv4_subnet.prefixlen}")

if __name__ == '__main__':
    starting_network = ipaddress.IPv4Network('10.100.0.0/20')
    next_net = next_network(starting_network)

    print(f'Starting network {starting_network}')
    print(f'Next network {next_net}')

Running the script we get the following output

$ python example_script.py 
Starting network 10.100.0.0/20
Next network 10.100.16.0/20
$

 

 



Comments (0)
Leave a Comment