#!/bin/bash mywget="/usr/bin/wget -q --tries=1 --spider" online_time_max=86400 # nach ~24h wird die Verbindung unzuverlaessig online_time_sleep=1000 # alle ~17 Minuten Verbindung prüfen online_delay=145 # ~2½ Minuten auf Router warten online_fail="maxtime" # Grund des beendens (>24h oder keine Verbindung) online_err_count=0 online_err_max=1 online_try_count=0 declare -a check_online_urls=( 'http://www.google.com' 'https://debianforum.de/forum' 'https://www.heise.de' 'https://www.debian.org' 'https://www.drei.at' 'https://www.tugraz.at/home') function log () { echo "${1}" | systemd-cat -p ${2} -t switchrouter } function switch () { case "${1}" in on) echo -n -e '\xA0\x01\x01\xA2' > /dev/ttyUSB0 ;; *) echo -n -e '\xA0\x01\x00\xA1' > /dev/ttyUSB0 ;; esac } function onsignal () { log "Received signal. Trying to switch off and exit" 6 switch off exit ${?} } trap onsignal SIGTERM SIGKILL # Router einschalten if switch on ; then log "Switched on router." 6 else log "Switching on router failed, trying to switch off nevertheless." 3 switch off exit 75 fi # Auf Router warten sleep ${online_delay} systemd-notify --ready #gefaehrlich! # Internetverbindung ueberwachen while [ ${SECONDS} -lt ${online_time_max} ] do for i in "${check_online_urls[@]}" ; do ${mywget} "${i}" && break (( online_try_count++ )) sleep 10s done || (( online_err_count++ )) if [ ${online_err_count} -gt ${online_err_max} ] ; then online_fail="connection" log "No internet connection (${online_try_count} tries)." 4 break else log "Internet connection ok (${online_try_count} fails)." 7 online_err_count=0 online_try_count=0 sleep ${online_time_sleep} fi done # Grund für das Beenden case "${online_fail}" in maxtime) log "Maximum connection time reached, exiting." 6 ;; connection) log "Exiting." 4 ;; esac # Router ausschalten if switch off ; then log "Switched off router." 6 else log "Switching off router failed." 2 exit 75 fi