Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if current connection is metered with NetworkManager

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

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

like image 546
phihag Avatar asked Apr 05 '17 10:04

phihag


People also ask

How do I find a metered network?

Select Start > Settings > Network & Internet > Wi-Fi > Manage known networks. Select the Wi-Fi network > Properties > turn on Set as metered connection.

How does Windows detect metered connection?

Windows detects connection as metered from 802.11 (Wi-Fi) beacons and probe responses. This means Windows will only see fast Wi-Fi connection and will not see that there is a slow 3G modem behind it. Solution is to tell Windows device that Wi-Fi connection should be treated as metered connection.

What is Metter connection?

A metered network is an internet plan where you pay to use a certain amount of data per month or per day and then pay a fee (usually around $10) for using more than your allotted data. Sometimes there's an option to throttle –– slow down and limit –– your data instead of charging for more data usage.


2 Answers

With the nmcli utility, the necessary steps are:

  1. verify NetworkManager is version 1.0.6+:

    $ nmcli -v nmcli tool, version 1.9.0

  2. check GENERAL.METERED on an interface:

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

  3. values are: unknown, yes, no, yes (guessed), no (guessed)

  4. Forcing the 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

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'`
like image 173
lossleader Avatar answered Oct 12 '22 09:10

lossleader


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

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

which is only one command, in contrast to the nmcli solution, and in other programming languages it can be more efficient to use D-Bus directly instead of having to call nmcli.

like image 44
ash Avatar answered Oct 12 '22 10:10

ash