Question

I am questioning the results of the ipcalc module (ipcalc) for Python (it seems that netaddr may be a better choice).

Let's take 192.168.1.25/30 as an example. In binary, the last octet is 00011001 AND 11111100 = 00011000, so I get 192.168.1.24 as the Network ID and the range 192.168.1.24 - 192.168.1.27.

Using ipcalc, when I specify

subnet = ipcalc.Network('192.168.1.25/30')
for x in subnet: print x

The output is

192.168.1.25
192.168.1.26
192.168.1.27
192.168.1.28

I am not understanding the inconsistency. When using CIDR notation, it seems that specifying both 192.168.1.24/30 and 192.168.1.25/30 (or .26/30 or .27/30) refer to the same subnet.

Is that correct? Is this just a bug in the ipcalc module?

Was it helpful?

Solution

There is an open bug for this at the moment: No way to resolve IP + Netmask to Network Object

And an earlier bug report that discuss the matter: Strange subnet calculations


But they have also added a function called network to get the network address from an IP. From the manual:

>>> localnet = Network('127.128.99.3/8')
>>> print localnet.network()
127.0.0.0

The manual specifically says that the constructor Network should take a network address as its first argument, not any IP in the network. Rather confusing if you ask me (especially since the above code block breaks that condition). I would at least read the code for the module before using it.

OTHER TIPS

It's correct the subnet is 192.168.1.24/30 so the ips 192.168.1.24 to 192.168.1.27 make part of this network.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top