checkconnection/checkconnection.sh

85 lines
1.5 KiB
Bash
Raw Permalink Normal View History

2023-08-22 20:14:01 +02:00
#!/bin/sh
2023-08-22 17:24:54 +02:00
#
## Variables
#
# Testsites should return HTTP-Code "204"
2023-08-22 18:13:16 +02:00
testsite1="https://datenkastl.de/.connectivity-check"
testsite2="http://captiveportal.kuketz.de"
2023-08-22 17:24:54 +02:00
# Testdomains for ping
2023-08-22 20:40:14 +02:00
testdomain1="datenkastl.de"
testdomain2="metager.de"
2023-08-22 17:24:54 +02:00
#Path of curl and ping
curlbin=`type -tp curl`
pingbin=`type -tp ping`
2023-08-22 18:13:16 +02:00
# Command in case of connection fail.
failcommand="echo Exit without action"
2023-08-22 17:24:54 +02:00
#
## Functions
#
# Check connetction with curl
testwithcurl() {
echo "Testing with curl..."
status=$(curl --write-out "%{http_code}\n" "$testsite1" --silent)
2023-08-22 17:24:54 +02:00
if [[ "$status" = "204" ]]; then
echo "The connection is up!"
else
status2=$(curl --write-out "%{http_code}\n" "$testsite2" --silent)
2023-08-22 17:24:54 +02:00
if [[ "$status2" = "204" ]]; then
echo "The network is up, but $testsite1 is down."
else
echo "The network is down."
2023-08-22 18:13:16 +02:00
$failcommand
2023-08-22 17:24:54 +02:00
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."
2023-08-22 18:13:16 +02:00
$failcommand
2023-08-22 17:24:54 +02:00
fi
}
#
## Main
#
2023-08-22 20:10:28 +02:00
# Set $curlbin and $pingbin to "0" when not installed (workaround for sh)
2023-08-22 20:27:01 +02:00
if [[ ! $curlbin ]]; then curlbin=0; fi
if [[ ! $pingbin ]]; then pingbin=0; fi
2023-08-22 17:24:54 +02:00
2023-08-22 20:10:28 +02:00
# Check network
2023-08-22 17:24:54 +02:00
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