eibgrad

ddwrt-ovpn-client-watchdog.sh

Apr 10th, 2021 (edited)
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.51 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: ddwrt-ovpn-client-watchdog.sh
  5. #       version: 4.2.0, 13-oct-2024, by eibgrad
  6. #       purpose: (re)start failed/stopped/unresponsive openvpn client
  7. #   script type: startup (autostart)
  8. #  installation:
  9. #    1. enable jffs2 (administration->jffs2)
  10. #    2. enable syslogd (services->services->system log)
  11. #    3. use shell (telnet/ssh) to execute one of the following commands:
  12. #         curl -kLs bit.ly/ddwrt-installer|tr -d '\r'|sh -s -- iNC273ER startup
  13. #       or
  14. #         wget -qO - bit.ly/ddwrt-installer|tr -d '\r'|sh -s -- iNC273ER startup
  15. #    4. (optional): use vi editor to modify options:
  16. #         vi /jffs/etc/config/ddwrt-ovpn-client-watchdog.startup
  17. #    5. reboot
  18. (
  19. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  20.  
  21. # time (in secs) between checks for failed/stopped/unresponsive openvpn client
  22. CHECK_INTERVAL=60
  23.  
  24. # remote host used for ping checks
  25. PING_HOST='8.8.8.8'
  26.  
  27. # time (in secs) between failed ping attempts
  28. PING_INTERVAL=10
  29.  
  30. # number of consecutive failed ping attempts required for restart
  31. PING_NUMFAIL=3 # (3 recommended, 0 disables ping checks)
  32.  
  33. # uncomment/comment to enable/disable
  34. #   when enabled, verify PING_HOST is reachable via bridge!
  35. #SW_PING_TAP_VIA_BRIDGE=
  36.  
  37. # ------------------------------- END OPTIONS -------------------------------- #
  38.  
  39. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  40.  
  41. LOCK="/tmp/$(basename $0).lock"
  42. OVPN_CONF='/tmp/openvpncl/openvpn.conf'
  43.  
  44. # function ping_check()
  45. ping_check() {
  46.     [ $PING_NUMFAIL -gt 0 ] || return 0
  47.  
  48.     local i=1
  49.     local vpn_if="$(awk '/^ *dev /{v=$2};END{print v}' $OVPN_CONF 2>/dev/null)"
  50.  
  51.     [ "$vpn_if" ] || { echo "error: line ${LINENO}: vpn_if=NULL"; return 0; }
  52.  
  53.     # we can't ping a bridged tunnel assigned to a bridge (e.g., br0)
  54.     if [ "${vpn_if:0:3}" == 'tap' ]; then
  55.         local br_if="$(echo /sys/class/net/*/brif/$vpn_if | cut -d/ -f5)"
  56.  
  57.         if [ "$br_if" != '*' ]; then
  58.             if [ ${SW_PING_TAP_VIA_BRIDGE+x} ]; then
  59.                 # ping bridge assignment instead of underlying tunnel
  60.                 vpn_if="$br_if"
  61.             else
  62.                 # skip ping checks
  63.                 return 0
  64.             fi
  65.         fi
  66.     fi
  67.  
  68.     # tip: ping multiple times to minimize risk of reporting false negative
  69.     while :; do
  70.         ping -qc1 -W3 -I $vpn_if $PING_HOST &>/dev/null && return 0
  71.         [ $((i++)) -ge $PING_NUMFAIL ] && break || sleep $PING_INTERVAL
  72.     done
  73.  
  74.     # fall-through == failure
  75.     return 1
  76. }
  77.  
  78. # reject additional instances
  79. mkdir $LOCK &>/dev/null || exit 0
  80.  
  81. # catch unexpected exit and cleanup
  82. trap "rmdir $LOCK; exit 0" SIGHUP SIGINT SIGTERM
  83.  
  84. # wait for *reliable* internet connection
  85. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  86.  
  87. while sleep $CHECK_INTERVAL; do
  88.     # openvpn client must be enabled
  89.     [ "$(nvram get openvpncl_enable)" != '0' ] || continue
  90.  
  91.     # check for failed/stopped/unresponsive openvpn client
  92.     [ "$(ps | awk '$0~v{print $1}' v="--config +$OVPN_CONF")" ] && \
  93.         ping_check && continue
  94.  
  95.     # confirm openvpn client is still enabled
  96.     [ "$(nvram get openvpncl_enable)" != '0' ] || continue
  97.  
  98.     # fall-through == failure; (re)start openvpn client
  99.     stopservice  openvpn && sleep 5
  100.     startservice openvpn && sleep 5
  101.     echo "info: openvpn client (re)started @ $(date)"
  102. done
  103.  
  104. ) 2>&1 | logger -t "$(basename $0 | grep -Eo '^.{0,23}')[$$]" &
Add Comment
Please, Sign In to add comment