Detect if the current connection is measured using NetworkManager

How can I determine if the current connection is established as measured on the system using NetworkManager?

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

+3


source to share


2 answers


Using the nmcli utility, the following steps are required:

  • check out NetworkManager version 1.0.6+ :

    $ nmcli -v nmcli tool, version 1.9.0

  • check GENERAL.METERED on interface:

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

  • values: unknown, yes, no, yes (guessed), no (guessed)

  • Coercing a value is done like this:

    $ 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

      

Attempting to shorten this to information only on the default route would require invoking a different command , since NetworkManager does not 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'`

      

+2


source


You can also get the measured status of the current connection via D-Bus. From the shell, you can use busctl

:

busctl get-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager Metered

      



this is just one command as opposed to a solution nmcli

, and in other programming languages ​​it might be more efficient to use D-Bus directly instead of calling it nmcli

.

0


source







All Articles