I highly recommend against using ping
to determine connectivity. There are too many network admins that disable ICMP (the protocol it uses) due to worries about ping flood attacks originating from their networks.
Instead, I use a quick test of a reliable server on a port you can expect to be open:
if nc -zw1 google.com 443; then
echo "we have connectivity"
fi
This uses netcat (nc
) in its port scan mode, a quick poke (-z
is zero-I/O mode [used for scanning]) with a quick timeout (-w 1
waits at most one second, though Apple OS X users may need to use -G 1
instead). It checks Google on port 443 (HTTPS).
I've used HTTPS rather than HTTP as an effort to protect against captive portals and transparent proxies which can answer on port 80 (HTTP) for any host. This is less likely when using port 443 since there would be a certificate mismatch, but it does still happen.
If you want to proof yourself against that, you'll need to validate the security on the connection:
test=google.com
if nc -zw1 $test 443 && echo |openssl s_client -connect $test:443 2>&1 |awk '
$1 == "SSL" && $2 == "handshake" { handshake = 1 }
handshake && $1 == "Verification:" { ok = $2; exit }
END { exit ok != "OK" }'
then
echo "we have connectivity"
fi
This checks for a connection (rather than waiting for openssl to time out) and then makes the SSL handshake, keying on the verification phase. It silently exits ("true") if the verification was "OK" or else exits with an error ("false"), then we report the finding.
The awk code analyzes the output of openssl
line by line:
- If the first word of the line is "SSL" and the second is "Verification", set
handshake
to 1
- If
handshake
is set and the first word of the line is "Verification",
then save the second word (the verification status) in ok
and stop reading
- Exit with a value of
0
(true) if the verification status was OK
, or else exit with 1
(false).
We use !=
here because shell exit codes are reversed
(An awk oddity: Running exit
while reading lines will simply stop reading lines and enter the END
condition, from which you can truly exit
.)
-d
e.g.nc -dzw1
also so it doesn't listen for STDIN and hang indefinetily in a script. and maybe use 8.8.8.8 instead of google.com to save a lookup.nc -dzw1 8.8.8.8 443
– dza Feb 17 '17 at 13:45-d
in my scripts, perhaps because I've never had an unused pipeline. That should be safe to add. – Adam Katz Feb 17 '17 at 17:05-w 1
timer I couldn't figure out what was going on but I guess it has something to do with network configuration. Odd however. If someone has an idea please reply. I tried port 80 also. – dza Feb 23 '17 at 21:59-w 1
still costs a second when there is no connectivity, though perhaps yournc
has some kind of obscure issue somwehere. If you have a recent version of nmap installed, you can instead doncat --send-only --recv-only -w 334ms
to cut that failure time to a third ofnc
(I've found that 334ms is a good wait time). – Adam Katz Feb 24 '17 at 19:39Ncat: Operation timed out.
even if I increased the timeout slowly up to 4000ms withncat --send-only --recv-only -w 334ms 8.8.8.8 443
and on nmap/ncat 6.40 Ubuntu it worked. – dza Feb 24 '17 at 23:47-G 1
instead of-w 1
. Apple'snc
seems to not use the-w
timeout at all for a zero i/o portscan and it just hangs when there's no connection. Like this:nc -zG 1 google.com 443
– Chris Apr 24 '20 at 03:15bash: nc: command not found
Archlinux base installation . – Salem F Aug 16 '21 at 17:45nc
) by default. Most distributions do, though it's not required by the LSB or POSIX. Arch has a number of different options, see https://wiki.archlinux.org/title/Network_tools#Netcat – Adam Katz Aug 16 '21 at 19:16man nc
still shows a "June 25, 2001" date. That doesn't necessarily mean that it's been that long since they did any maintenance ofnc
itself, but they have a sad reputation for letting things rot. And Apple'snc
emits this annoying string that has to be dealt with in scripting:Connection to google.com port 443 [tcp/https] succeeded!
. – Seamus Mar 17 '24 at 03:59nc -zG 1 google.com 443 >/dev/null 2>&1
– Adam Katz Mar 18 '24 at 14:51socat
lends itself to more compact implementations. – Seamus Mar 18 '24 at 20:21nc
, notsocat
, and @Seamus uses macOS. – huyz Mar 19 '24 at 20:48