Pinging the current gateway

Tags:

Connecting to wireless networks can be a bit of a trial-and-error thing, and the "number of bars" indicator does not tell you the whole story. Here's a single command-line for Linux that pings the current gateway -- so you can try rotating or moving your computer to see how the reception changes.

ping `route -n | grep "^0.0.0.0" | tr -s ' ' | cut -d ' '  -f2`

Here we are nesting the output of a command with the back-quotes. We get the current route table which looks something like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     2      0        0 wlan0
0.0.0.0         192.168.1.254   0.0.0.0         UG    0      0        0 wlan0

and then use grep to extract just the line that starts with the universal destination 0.0.0.0, the next field being the IP address of the gateway. Then tr compresses multiple spaces to one, and cut selects just the second field... returning that to ping.

The end result looks like:

$ ping `route -n | grep "^0.0.0.0" | tr -s ' ' | cut -d ' '  -f2`
PING 192.168.1.254 (192.168.1.254) 56(84) bytes of data.
64 bytes from 192.168.1.254: icmp_seq=1 ttl=64 time=2.01 ms
64 bytes from 192.168.1.254: icmp_seq=2 ttl=64 time=1.52 ms
64 bytes from 192.168.1.254: icmp_seq=3 ttl=64 time=1.42 ms
64 bytes from 192.168.1.254: icmp_seq=4 ttl=64 time=1.34 ms

so you get an immediate feedback of times, and timeouts, duplicate packets, and so on. Press Ctrl+C to exit ping.