checkconnection/checkconnection.sh

84 lines
1.5 KiB
Bash
Executable file

#!/bin/sh
#
## Variables
#
# Testsites should return HTTP-Code "204"
testsite1="https://datenkastl.de/.connectivity-check"
testsite2="http://captiveportal.kuketz.de"
# Testdomains for ping
testdomain1="datenkastl.de"
testdomain2="metager.de"
#Path of curl and ping
curlbin=`type -tp curl`
pingbin=`type -tp ping`
# Command in case of connection fail.
failcommand="echo Exit without action"
#
## Functions
#
# Check connetction with curl
testwithcurl() {
echo "Testing with curl..."
status=$(curl --write-out "%{http_code}\n" "$testsite1" --silent)
if [[ "$status" = "204" ]]; then
echo "The connection is up!"
else
status2=$(curl --write-out "%{http_code}\n" "$testsite2" --silent)
if [[ "$status2" = "204" ]]; then
echo "The network is up, but $testsite1 is down."
else
echo "The network is down."
$failcommand
fi
fi
}
#Check connection with ping
testwithping() {
echo "Testing with ping..."
if ping -q -c 1 -W 1 $testdomain1 >/dev/null; then
echo "The connection is up."
elif ping -q -c 1 -W 1 $testdomain2 >/dev/null; then
echo "The network is up, but $testdomain1 is down."
else
echo "The network is down."
$failcommand
fi
}
#
## Main
#
# Set $curlbin and $pingbin to "0" when not installed (workaround for sh)
if [[ ! $curlbin ]]; then curlbin=0; fi
if [[ ! $pingbin ]]; then pingbin=0; fi
# Check network
if [[ -x ${curlbin} ]]; then
testwithcurl
elif [[ -x ${pingbin} ]]; then
testwithping
else
echo "Can't find curl or ping. Please install one of them."
exit 1
fi