eibgrad

ddwrt-ovpn-server-watchdog.sh

Sep 28th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.94 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG=; set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: ddwrt-ovpn-server-watchdog.sh
  5. #       version: 1.2.0, 02-oct-2024, by eibgrad
  6. #       purpose: (re)start failed/stopped openvpn server
  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 -- 4QyiANWh startup
  13. #       or
  14. #         wget -qO - bit.ly/ddwrt-installer|tr -d '\r'|sh -s -- 4QyiANWh startup
  15. #    4. (optional): use vi editor to modify options:
  16. #         vi /jffs/etc/config/ddwrt-ovpn-server-watchdog.startup
  17. #    5. reboot
  18. (
  19. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  20.  
  21. # time (in secs) between checks for failed/stopped openvpn server
  22. CHECK_INTERVAL=60
  23.  
  24. # ------------------------------- END OPTIONS -------------------------------- #
  25.  
  26. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  27.  
  28. LOCK="/tmp/$(basename $0).lock"
  29. OVPN_CONF='/tmp/openvpn/openvpn.conf'
  30.  
  31. # reject additional instances
  32. mkdir $LOCK &>/dev/null || exit 0
  33.  
  34. # catch unexpected exit and cleanup
  35. trap "rmdir $LOCK; exit 0" SIGHUP SIGINT SIGTERM
  36.  
  37. # wait for *reliable* internet connection
  38. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  39.  
  40. while sleep $CHECK_INTERVAL; do
  41.     # openvpn server must be enabled
  42.     [ "$(nvram get openvpn_enable)" != '0' ] || continue
  43.  
  44.     # check for failed/stopped openvpn server
  45.     [ "$(ps | awk '$0~v{print $1}' v="--config +$OVPN_CONF")" ] && continue
  46.  
  47.     # fall-through means failure; restart the openvpn server
  48.     stopservice  openvpnserver && sleep 5
  49.     startservice openvpnserver && sleep 5
  50.     echo "info: openvpn server (re)started @ $(date)"
  51. done
  52.  
  53. ) 2>&1 | logger -t "$(basename $0) | grep -Eo '^.{0,23}')[$$]" &
Add Comment
Please, Sign In to add comment