blob: bb2f766333fe02bb86981e6efbb2dac85a44fbb7 [file] [log] [blame]
#!/bin/sh
# Check whether an interface has an internet connection by pinging both
# HonestDNS servers over both IPV4 and IPV6. If either succeeds, we must be
# connected to the internet.
HONEST_DNS="8.8.4.4 8.8.8.8"
HONEST_DNS6="2001:4860:4860::8844 2001:4860:4860::8888"
usage() {
echo "Check whether an interface is connected to the internet."
echo "With -a, checks for access to the ACS instead."
echo ""
echo "connection_check -I <interface>"
echo "connection_check -a -I <interface>"
}
while getopts "I:a" option
do
case $option in
I)
interface="$OPTARG" ;;
a)
USE_ACS=true ;;
*)
usage
exit 99 ;;
esac
done
if [ -z "$interface" ]; then
usage
exit 99
fi
if [ $USE_ACS ]; then
ACS="$(set-acs print 2>/dev/null | pcregrep -o https:\/\/[^/]+)"
curl --interface "$interface" "$ACS/generate_204"
exit $?
fi
for IP in $HONEST_DNS; do
if ping $IP -c1 -I "$interface" >/dev/null 2>&1; then exit 0; fi
done
for IP in $HONEST_DNS6; do
if ping6 $IP -c1 -I "$interface" >/dev/null 2>&1; then exit 0; fi
done
exit 1