简体   繁体   English

.netifaces.gateways”如何选择默认网关?

[英]How "netifaces.gateways" choose default gateway?

I have 2.network cards on my linux pc, and with next route settings:我的 linux 电脑上有 2.network 卡,并且有下一个路由设置:

$ 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:Metrics .networking)中,它说:

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.该路由将 go 指向具有最低度量的网关。

So, I think the gateway 10.192.244.254 has high priority.所以,我认为网关10.192.244.254具有高优先级。 But, with next python code:但是,使用下一个 python 代码:

try.py:尝试.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?你可以看到它只是告诉我192.168.100.1是默认路由器,为什么它不选择10.192.244.254或显示 2 个默认路由器?

I found I'm on debian 10 , while the version of python3.netifaces is an old version:我发现我在debian 10上,而python3.netifaces的版本是旧版本:

ii  python3-netifaces 0.10.4-1+b1  amd64

On debian 11 , which used the new library version as next, it works as expected:在下一个使用新库版本的debian 11上,它按预期工作:

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. 由此,我可以确认我的问题是旧版本的错误,并已在 2020/12/17 的新netifaces版本中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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