eibgrad

merlin-ovpn-server-restart.sh

Aug 5th, 2022 (edited)
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.21 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG= # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: merlin-ovpn-server-restart.sh
  5. #       version: 1.1.0, 05-aug-2022, by eibgrad
  6. #       purpose: restart openvpn server(s) on bootup
  7. #       type(s): services-start
  8. #          href: https://tinyurl.com/4bnrte9c
  9. #  installation:
  10. #    1. enable jffs custom scripts and configs (administration->system)
  11. #    2. ssh to router and copy/paste the following command:
  12. #         curl -kLs bit.ly/merlin-installer|tr -d '\r'|sh -s MkKb9tia
  13. #    3. reboot
  14.  
  15. SCRIPTS_DIR='/jffs/scripts'
  16. SCRIPT1="$SCRIPTS_DIR/merlin-ovpn-server-restart.sh"
  17. SCRIPT2="$SCRIPTS_DIR/services-start"
  18.  
  19. mkdir -p $SCRIPTS_DIR
  20.  
  21. # --------------------- begin merlin-ovpn-server-restart --------------------- #
  22. cat << 'EOF' > $SCRIPT1
  23. #!/bin/sh
  24. #set -x # comment/uncomment to disable/enable debug mode
  25. {
  26. if [ ! "$1" ]; then
  27.     # only autostart enabled openvpn servers need to be considered
  28.     for i in $(nvram get vpn_serverx_start | tr ',' ' '); do
  29.         nohup $0 $i &>/dev/null &
  30.     done
  31. else
  32.     # wait for successful openvpn server startup
  33.     until pidof vpnserver${1} &>/dev/null && \
  34.             [ "$(nvram get vpn_server${1}_state)" == '2' ]
  35.         do sleep 10; done; sleep 10
  36.  
  37.     # restart the openvpn server
  38.     service restart_vpnserver${1} >/dev/null
  39.     echo "openvpn server #$1 restarted @ $(date)"
  40. fi
  41.  
  42. exit 0
  43. } 2>&1 | logger -t $(basename $0 .sh)[$$]
  44. EOF
  45. [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT1
  46. chmod +x $SCRIPT1
  47. echo "installed: $SCRIPT1"
  48. # ---------------------- end merlin-ovpn-server-restart ---------------------- #
  49.  
  50. # --------------------------- begin services-start --------------------------- #
  51. create_script() {
  52. cat << 'EOF' > $SCRIPT2
  53. #!/bin/sh
  54. #set -x # comment/uncomment to disable/enable debug mode
  55. {
  56. $SCRIPT1
  57. } 2>&1 | logger -t $(basename $0)[$$]
  58. EOF
  59. [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT2
  60. sed "s:\$SCRIPT1:$SCRIPT1:g" -i $SCRIPT2
  61. chmod +x $SCRIPT2
  62. }
  63.  
  64. if [ -f $SCRIPT2 ]; then
  65.     echo "error: $SCRIPT2 already exists; requires manual installation"
  66. else
  67.     create_script
  68.     echo "installed: $SCRIPT2"
  69. fi
  70. # ---------------------------- end services-start ---------------------------- #
Add Comment
Please, Sign In to add comment