本文介绍了检测是否使用NetworkManager计量当前连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用NetworkManager的系统上检测当前连接是否标记为已计量?

How can I detect whether the current connection is marked as metered on a system with NetworkManager?

这是来自shell脚本的,但是我可以通过Python轻松地调用任何C函数.

This is from a shell script, but I can easily call any C functions via Python.

推荐答案

使用nmcli实用程序,必要步骤如下:

With the nmcli utility, the necessary steps are:

  1. 验证NetworkManager是否为版本1.0.6+ :

$ nmcli -vnmcli tool, version 1.9.0

$ nmcli -vnmcli tool, version 1.9.0

在接口上检查GENERAL.METERED:

check GENERAL.METERED on an interface:

$ nmcli -t -f GENERAL.METERED dev show eth1GENERAL.METERED:unknown

$ nmcli -t -f GENERAL.METERED dev show eth1GENERAL.METERED:unknown

值是:unknown, yes, no, yes (guessed), no (guessed)

强制值是这样完成的:

$ nmcli dev modify wlan1 connection.METERED yes Connection successfully reapplied to device 'wlan1' $ nmcli -t -f GENERAL.METERED dev show wlan1 GENERAL.METERED:yes

$ nmcli dev modify wlan1 connection.METERED yes Connection successfully reapplied to device 'wlan1' $ nmcli -t -f GENERAL.METERED dev show wlan1 GENERAL.METERED:yes

然后,要获取按设备分组的列表:

And, to get a list grouped by device:

  $ nmcli -t -f GENERAL.DEVICE,GENERAL.METERED dev show

  GENERAL.DEVICE:wlan1
  GENERAL.METERED:yes

  GENERAL.DEVICE:eth1
  GENERAL.METERED:unknown

  GENERAL.DEVICE:lo
  GENERAL.METERED:unknown

尝试仅通过默认路由将其缩减为信息,仍然需要,因为NetworkManager不会尝试区分处于连接状态的多个设备:

Trying to cut this down to info on just the default route would still require a call to another command as NetworkManager doesn't try to distinguish between multiple devices in a connected state:

  $ nmcli -t -f GENERAL.DEVICE,GENERAL.METERED dev show `ip route list 0/0 | sed -r 's/.*dev (\S*).*/\1/g'`

这篇关于检测是否使用NetworkManager计量当前连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 08:13