简体   繁体   中英

How to get the next network address given the starting network address in python

Is there a way in python to get the next network address from the given start network address. There are ways go get the next host IP using the ipaddress module, but is there a way to get the next network itself?

For example, if my start network address 10.1.0.0/16 , then on each iteration I would like to get the next network address as 10.2.0.0/16 , 10.3.0.0/16 and so on.

So in case of 10.1.0.0/16 the network part is the first two octets(10.1) and remaining are the host part. I want the network part alone to be incremented on each iteration, leaving host part as it is, for eg:

start_network = ipaddress.ip_network('10.1.0.0/16')

# Now on each iteration I want the below output

In [37]: next(start_network)
Out[37]: IPv4Network('10.2.0.0/16')
In [38]: next(start_network)
Out[38]: IPv4Network('10.3.0.0/16')

Tried using subnets options, but as name suggests it will get you the sub networks within the current network

In [34]: network = ipaddress.ip_network('10.1.0.0/16')

In [35]: network.subnets()

But our requirement is to get the next network address (preferably without any string manipulations)

The library ipcalc has routines to make math on ip addresses fairly easy. As an example an iterator to get the next network range address can be done like:

Code:

import ipcalc
import itertools as it

network = ipcalc.Network('10.1.0.0/16')
network_addrs = (network + (i + 1) * network.size() for i in it.count())

Test Code:

print(next(network_addrs))
print(next(network_addrs))
print(next(network_addrs))

Results:

10.2.0.0/16
10.3.0.0/16
10.4.0.0/16

Standard Library Only:

If it would be preferable to not install ipcalc , a class that inherits from ipaddress.IPv4Network can be constructed.

import ipaddress

class BetterIPv4Network(ipaddress.IPv4Network):

    def __add__(self, offset):
        """Add numeric offset to the IP."""
        new_base_addr = int(self.network_address) + offset
        return self.__class__((new_base_addr, str(self.netmask)))

    def size(self):
        """Return network size."""
        start = int(self.network_address)
        return int(self.broadcast_address) + 1 - start

Usage:

import itertools as it

network = BetterIPv4Network(u'10.1.0.0/16')
network_addrs = (network + (i + 1) * network.size() for i in it.count())

Python 3.4:

Python 3.4 did not accept tuples to init ipaddress.IPv4Network . This code will work around that.

class BetterIPv4Network(ipaddress.IPv4Network):

    def __add__(self, offset):
        """Add numeric offset to the IP."""
        new_base_addr = int(self.network_address) + offset
        new_base_addr_str = str(self.__class__(new_base_addr)).split('/')[0]
        return self.__class__(new_base_addr_str + '/' + str(self.netmask))

    def size(self):
        """Return network size."""
        start = int(self.network_address)
        return int(self.broadcast_address) + 1 - start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM