TR;DR: use nmcli dev status
to get a list of which interfaces are connected to the network, and then use ping google.com -c 4 -I ifacex
(where ifacex is actual interface name with number) to test if they are actually connected. To reconnect wireless interfaces nmcli nm wifi off; sleep 1; nmcli nm wifi on
and let it automatically reconnect.
The simplest way to do so is through nmcli dev status
. This command outputs the list of available interfaces on your system and tells you whether they are connected or not. Sample output from my system
DEVICE TYPE STATE
eth0 802-3-ethernet unavailable
wlan0 802-11-wireless connected
If you'd wanna list only the devices which state is connected, then do
nmcli dev status | awk '/connected/ {print $1}'
. Here we match only lines that have 'connected' string in them, and print their name. In my case, it would give wlan0
only. If you'd have both eth0 and wlan0 connected, sample output would be
wlan0
eth0
Now , on my system network manager is kind of buggy. It may report device as connected, but in actuality it is disconnected.
You could test wlan0 interface with ping google.com -c 4 -I wlan0
. This sends 4 packets to google.com
from interface wlan0. Now, I could also do
nmcli dev status | awk '/connected/ {print $1}' | xargs ping google.com -c 4 -I
, which will get the name of my wlan0 interface and stick it on the end of ping command as -I argument. Sample output
PING google.com (216.58.217.46) from 192.168.0.68 wlan0: 56(84) bytes of data.
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=1 ttl=57 time=44.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=2 ttl=57 time=40.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=3 ttl=57 time=48.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=4 ttl=57 time=40.0 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3016ms
rtt min/avg/max/mdev = 40.000/43.000/48.000/3.316 ms
Often users might have dns issue, which means your machine cannot translate google.com to actual ip address (computers deal only in ip addresses, like phones only deal with phone numbers). In that case, you'd better run ping 8.8.8.8 -c 4 -I wlan0 && ping google.com -c 4 -I wlan0
, where it will test if your machine has connection to the web AND resolves domain names.
Now, what if you have more than one connected interface ? Personally I'd test each interface with ping separately, without relying on that one-liner, but because I am having so much fun with scripting recently, here's another one-liner:
nmcli dev status | awk '/connected/ {print $1}' | xargs -n 1 ping google.com -c 4 -I
This one , basically finds names of connected interfaces, and executes ping command with each one. Since I don't have multiple devices connected, I'll just show you how it looks when I awk gets each item from first column, so note , the first output will be wrong, second is eth0, and third wlan0
nmcli dev status | awk '{print $1}' | xargs -n 1 ping google.com -c 4 -I
ping: SO_BINDTODEVICE: Invalid argument
ping: Warning: source address might be selected on device other than eth0.
PING google.com (216.58.217.46) from 192.168.0.68 eth0: 56(84) bytes of data.
From sergiy-Satellite-L455D.local (192.168.0.68) icmp_seq=1 Destination Host Unreachable
From sergiy-Satellite-L455D.local (192.168.0.68) icmp_seq=2 Destination Host Unreachable
From sergiy-Satellite-L455D.local (192.168.0.68) icmp_seq=3 Destination Host Unreachable
From sergiy-Satellite-L455D.local (192.168.0.68) icmp_seq=4 Destination Host Unreachable
--- google.com ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3152ms
pipe 3
PING google.com (216.58.217.46) from 192.168.0.68 wlan0: 56(84) bytes of data.
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=1 ttl=57 time=44.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=2 ttl=57 time=48.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=3 ttl=57 time=40.0 ms
64 bytes from den03s10-in-f14.1e100.net (216.58.217.46): icmp_seq=4 ttl=57 time=40.0 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3016ms
rtt min/avg/max/mdev = 40.000/43.000/48.000/3.316 ms
And as it should, we see eth0 is disconnected, wlan0 is connected.
Another thing that you could use is nm-tool
. This guy gives you long report about each one of your interfaces, with settings. We could cut that down to only the list of interfaces and their statuses with nm-tool | tr '-' ' ' | awk '/Device/,/State/ {print $1,$2}'
. Here we substitute the annoying - character with space, and just get the text from Device to State strings.
Device: eth0
Type: Wired
Driver: r8169
State: unavailable
Device: wlan0
Type: 802.11
Driver: r8180
State: connected
What else ?
whois
,nslookup
and dig
are lookup utilities for domain names and ip addresses. If you can use them to find out info about a domain or ip address, you're connected. They won't tell you the interface, but nice to actually test the connectivity.
In conclusion: There's more than one way to skin a cat, as you know. The very first command nmcli dev status
and ping
is all you really need to test your connections, and ping is the "real MVP" on any system, so to speak. Now, if you wanna get fancy, learn some new command line tricks, by all means study the one-liners I've posted here ! The power of Ubuntu and Linux in general lies in command line
Additions:
Other's have appropriately noted that restarting network manager with sudo service network-manager restart
will do the job most of the time. By default all connections are set to get autoconnected when network manager is in range. I use this method a lot !
What else can be done ? nmcli nm wifi off; sleep 1; nmcli nm wifi on
. This will turn off wifi , pause for a second, and turn on wifi again; and your network should automatically reconnect.