Many corporate firewalls block ICMP
- which is the protocol is used by ping
utility.
Better solution for this is to try TCP
connection to google.com:80
.
Easiest way to check for basic internet connectivity in shell script is utility nc
(it should be easily available on most Linux systems):
nc -w 3 -z google.com 80
echo $?
This means check if port 80
on google.com
is open, and timeout after 3
seconds. If connection was successful, it will print 0
, and if it failed, it will print 1
.
If you want to check internet connectivity without checking DNS (which could be broken in itself), you can use Google's preferred DNS server 8.8.8.8
, but the only port that it has open is 53
(aka domain
):
nc -w 3 -z 8.8.8.8 53
echo $?
However, port 53
can be also blocked by your corporate firewall (not common, but possible). Ports 80
and 443
, on other hand, are almost never firewalled.