pythonlinuxpython-netifaces

How "netifaces.gateways" choose default gateway?


I have 2 network cards on my linux pc, and with next route settings:

$ ip route
default via 10.192.244.254 dev enp0s31f6 proto static metric 100
default via 192.168.100.1 dev enp2s0 proto dhcp src 192.168.100.106 metric 200
10.192.244.0/24 dev enp0s31f6 proto kernel scope link src 10.192.244.193
169.254.0.0/16 dev docker0 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.100.0/23 dev enp2s0 proto kernel scope link src 192.168.100.106
192.168.100.1 dev enp2s0 proto dhcp scope link src 192.168.100.106 metric 200

In Metrics (networking), it said:

Router metrics are metrics used by a router to make routing decisions. A metric is typically one of many fields in a routing table. Router metrics help the router choose the best route among multiple feasible routes to a destination. The route will go in the direction of the gateway with the lowest metric.

So, I think the gateway 10.192.244.254 has high priority. But, with next python code:

try.py:

import netifaces
gateways = netifaces.gateways()
print(gateways)

execution:

$ python3 try.py
{'default': {2: ('192.168.100.1', 'enp2s0')}, 2: [('10.192.244.254', 'enp0s31f6', True), ('192.168.100.1', 'enp2s0', True)]}

You can see it just tell me 192.168.100.1 is the default router, why it not choose 10.192.244.254 or show me 2 default routers?


Solution

  • I found I'm on debian 10, while the version of python3-netifaces is an old version:

    ii  python3-netifaces 0.10.4-1+b1  amd64
    

    On debian 11, which used the new library version as next, it works as expected:

    ii  python3-netifaces 0.10.9-0.2+b3 amd64
    

    execution:

    # python3
    Python 3.9.2 (default, Feb 28 2021, 17:03:44)
    [GCC 10.2.1 20210110] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import netifaces
    >>> gateways = netifaces.gateways()
    >>> print(gateways)
    {'default': {2: ('10.192.244.254', 'enp0s31f6')}, 2: [('10.192.244.254', 'enp0s31f6', True), ('192.168.100.1', 'enp2s0', False)]}
    >>>
    

    From this, I could confirm my issue is a bug of old version and fixed on 2020/12/17 in new netifaces version.