0

I'm trying to validate IP, gateway and subnet mask inputs using the ipaddress library, however I'm getting invalid results when I'm actually trying to use the validated network, which makes me think the validation is incorrect.

network = ipaddress.ip_network("103.162.136.145/255.255.255.252", strict=False)
for ip in ipaddress.ip_network(network):
    print(ip)

The following gives these valid IPs

103.162.136.144
103.162.136.145
103.162.136.146
103.162.136.147

However when I try to use it in linux, I get the following error;

localhost:~# ip addr flush dev eth0
localhost:~# ip addr add 103.162.136.145/255.255.255.252 dev ethd localhost:~# ip link set ethO up
localhost:~# ip route replace default via 103.162.136.144
ip: RTNETLINK answers: Invalid argument

The correct range shown should be actually

103.162.136.145
103.162.136.146

What am I doing wrong in the validation?

Edit - 103.162.136.145/255.255.255.252 is ip/subnet mask and 103.162.136.144 is gateway

5
  • 2
    The first looks like the network address, and the last a broadcast? IDK, but maybe the lib is treating it as valid addresses of the network? I'm not sure what's up with the other parts Commented Feb 13, 2024 at 14:57
  • @Greg Sorry, added an edit stating what they are Commented Feb 13, 2024 at 15:01
  • @Greg I misunderstood, you mean out of the 4 addresses, yeah, that does seem so, but why is it doing it? Commented Feb 13, 2024 at 15:08
  • 1
    In a network the first IP address is the network address and the last address is the broadcast address. They are part of the address space but no valid host addresses. That's means the gateway address you gave is wrong for this network. Commented Feb 13, 2024 at 15:34
  • @user2384330 "But why is it doing it?" check my answer, please stackoverflow.com/a/77989375/299774 Commented Feb 13, 2024 at 16:11

1 Answer 1

2

As mentioned in the comment, this library seems to return the network address and the broadcast address which are the two additional addresses you did not expect. You then ask:

that does seem so, but why is it doing it?

This is an excellent question, but to the authors of the library. One way to pick their brain is to read their friendly manual.

You did not specify the python version, but I've found this for python3:

Network objects can be iterated to list all the addresses belonging to the network. For iteration, all hosts are returned, including unusable hosts (for usable hosts, use the hosts() method)

https://docs.python.org/3/library/ipaddress.html#iteration

So, instead of iterating over this network object - use a method that returns the usable addresses?

I'm not a Python programmer, but something like this, maybe?

for ip in network.hosts():
    print(ip)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.