How can an internet connection be tested without pinging some website? I mean, what if there is a connection but the site is down? Is there a check for a connection with the
This is notably faster than any solution posted here, and can be used to test a specific host too.
The trick is solving the IP using a less busy DNS solver than the one employed by ping
:
validateConnection () {
host="${1}"
ip=$(getent ahostsv4 "${host}" | awk '{ print $1 }' | head -n1)
ping -c1 "${ip}" 2>&1 >"/dev/null"
if [ "${?}" -ne 0 ]; then
echo "No connection to: ${host}" >&2
exit 1
fi
}