Shell Scripting: Proper way to check for internet connectivity?
Testing IPv4 connectivity
If your network lets pings through, try pinging 8.8.8.8 (a server run by Google).
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
echo "IPv4 is up"
else
echo "IPv4 is down"
fi
Testing IP connectivity and DNS
If you only want the test to succeed when DNS is also working, use a host name.
if ping -q -c 1 -W 1 google.com >/dev/null; then
echo "The network is up"
else
echo "The network is down"
fi
Testing web connectivity
Some firewalls block pings. Some places have a firewall that blocks all traffic except via a web proxy. If you want to test web connectivity, you can make an HTTP request.
case "$(curl -s --max-time 2 -I http://google.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) echo "HTTP connectivity is up";;
5) echo "The web proxy won't let us through";;
*) echo "The network is down or very slow";;
esac
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 '
handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 }
$1 $2 == "SSLhandshake" { handshake = 1 }'
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.
I made a script that uses multiple ways to check internet connection (ping, nc, and curl, thanks to Adam Katz, Gilles, and Archemar). I hope someone finds this useful. Feel free to edit it to your liking/optimize it.
Checks your gateway, DNS, and internet connection (using curl, nc, and ping).
Put this in a file then make it executable (Usually sudo chmod +x filename
)
#!/bin/bash
GW=`/sbin/ip route | awk '/default/ { print $3 }'`
checkdns=`cat /etc/resolv.conf | awk '/nameserver/ {print $2}' | awk 'NR == 1 {print; exit}'`
checkdomain=google.com
#some functions
function portscan
{
tput setaf 6; echo "Starting port scan of $checkdomain port 80"; tput sgr0;
if nc -zw1 $checkdomain 80; then
tput setaf 2; echo "Port scan good, $checkdomain port 80 available"; tput sgr0;
else
echo "Port scan of $checkdomain port 80 failed."
fi
}
function pingnet
{
#Google has the most reliable host name. Feel free to change it.
tput setaf 6; echo "Pinging $checkdomain to check for internet connection." && echo; tput sgr0;
ping $checkdomain -c 4
if [ $? -eq 0 ]
then
tput setaf 2; echo && echo "$checkdomain pingable. Internet connection is most probably available."&& echo ; tput sgr0;
#Insert any command you like here
else
echo && echo "Could not establish internet connection. Something may be wrong here." >&2
#Insert any command you like here
# exit 1
fi
}
function pingdns
{
#Grab first DNS server from /etc/resolv.conf
tput setaf 6; echo "Pinging first DNS server in resolv.conf ($checkdns) to check name resolution" && echo; tput sgr0;
ping $checkdns -c 4
if [ $? -eq 0 ]
then
tput setaf 6; echo && echo "$checkdns pingable. Proceeding with domain check."; tput sgr0;
#Insert any command you like here
else
echo && echo "Could not establish internet connection to DNS. Something may be wrong here." >&2
#Insert any command you like here
# exit 1
fi
}
function httpreq
{
tput setaf 6; echo && echo "Checking for HTTP Connectivity"; tput sgr0;
case "$(curl -s --max-time 2 -I $checkdomain | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) tput setaf 2; echo "HTTP connectivity is up"; tput sgr0;;
5) echo "The web proxy won't let us through";exit 1;;
*)echo "Something is wrong with HTTP connections. Go check it."; exit 1;;
esac
# exit 0
}
#Ping gateway first to verify connectivity with LAN
tput setaf 6; echo "Pinging gateway ($GW) to check for LAN connectivity" && echo; tput sgr0;
if [ "$GW" = "" ]; then
tput setaf 1;echo "There is no gateway. Probably disconnected..."; tput sgr0;
# exit 1
fi
ping $GW -c 4
if [ $? -eq 0 ]
then
tput setaf 6; echo && echo "LAN Gateway pingable. Proceeding with internet connectivity check."; tput sgr0;
pingdns
pingnet
portscan
httpreq
exit 0
else
echo && echo "Something is wrong with LAN (Gateway unreachable)"
pingdns
pingnet
portscan
httpreq
#Insert any command you like here
# exit 1
fi