NoPaste

switchrouter

von smutbert

SNIPPET_TEXT:
  1. #!/bin/bash
  2.  
  3. mywget="/usr/bin/wget -q --tries=1 --spider"
  4.  
  5. online_time_max=86400   # nach ~24h wird die Verbindung unzuverlaessig
  6. online_time_sleep=1000  # alle ~17 Minuten Verbindung prüfen
  7. online_delay=145        # ~2½ Minuten auf Router warten
  8.  
  9. online_fail="maxtime"   # Grund des beendens (>24h oder keine Verbindung)
  10. online_err_count=0
  11. online_err_max=1
  12. online_try_count=0
  13.  
  14. declare -a check_online_urls=(  'http://www.google.com'
  15.                                 'https://debianforum.de/forum'
  16.                                 'https://www.heise.de'
  17.                                 'https://www.debian.org'
  18.                                 'https://www.drei.at'
  19.                                 'https://www.tugraz.at/home')
  20.  
  21.  
  22. function log ()
  23. {
  24.         echo "${1}" | systemd-cat -p ${2} -t switchrouter
  25. }
  26.  
  27. function switch ()
  28. {
  29.         case "${1}" in
  30.                 on)
  31.                         echo -n -e '\xA0\x01\x01\xA2' > /dev/ttyUSB0
  32.                         ;;
  33.                 *)
  34.                         echo -n -e '\xA0\x01\x00\xA1' > /dev/ttyUSB0
  35.                         ;;
  36.         esac
  37. }
  38.  
  39. function onsignal ()
  40. {
  41.         log "Received signal. Trying to switch off and exit" 6
  42.         switch off
  43.         exit ${?}
  44. }
  45.  
  46. trap onsignal SIGTERM SIGKILL
  47.  
  48. # Router einschalten
  49. if switch on ; then
  50.         log "Switched on router." 6
  51. else
  52.         log "Switching on router failed, trying to switch off nevertheless." 3
  53.         switch off
  54.         exit 75
  55. fi
  56.  
  57. # Auf Router warten
  58. sleep ${online_delay}
  59. systemd-notify --ready  #gefaehrlich!
  60.  
  61. # Internetverbindung ueberwachen
  62. while [ ${SECONDS} -lt ${online_time_max} ]
  63. do
  64.         for i in "${check_online_urls[@]}" ; do
  65.                 ${mywget} "${i}" && break
  66.                 (( online_try_count++ ))
  67.                 sleep 10s
  68.         done || (( online_err_count++ ))
  69.         if [ ${online_err_count} -gt ${online_err_max} ] ; then
  70.                 online_fail="connection"
  71.                 log "No internet connection (${online_try_count} tries)." 4
  72.                 break
  73.         else
  74.                 log "Internet connection ok (${online_try_count} fails)." 7
  75.                 online_err_count=0
  76.                 online_try_count=0
  77.                 sleep ${online_time_sleep}
  78.         fi
  79. done
  80.  
  81. # Grund für das Beenden
  82. case "${online_fail}" in
  83.         maxtime)
  84.                 log "Maximum connection time reached, exiting." 6
  85.         ;;
  86.         connection)
  87.                 log "Exiting." 4
  88.         ;;
  89. esac
  90.  
  91. # Router ausschalten
  92. if switch off ; then
  93.         log "Switched off router." 6
  94. else
  95.         log "Switching off router failed." 2
  96.         exit 75
  97. fi

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN