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.
With the nmcli utility, the necessary steps are:
verify NetworkManager is version 1.0.6+:
$ nmcli -v
nmcli tool, version 1.9.0
check GENERAL.METERED on an interface:
$ nmcli -t -f GENERAL.METERED dev show eth1
GENERAL.METERED:unknown
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'`