Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #DEBUG= # uncomment/comment to enable/disable debug mode
- # name: merlin-ovpn-client-watchdog.sh
- # version: 3.0.1, 05-aug-2022, by eibgrad
- # purpose: restart failed/stopped/unresponsive openvpn clients
- # type(s): services-start
- # href: https://tinyurl.com/2p9a68ne
- # installation:
- # 1. enable jffs custom scripts and configs (administration->system)
- # 2. ssh to router and copy/paste the following command:
- # curl -kLs bit.ly/merlin-installer|tr -d '\r'|sh -s wyKu0pww
- # 3. modify script w/ your preferred options using nano editor:
- # nano /jffs/scripts/merlin-ovpn-client-watchdog.sh
- # 4. reboot
- SCRIPTS_DIR='/jffs/scripts'
- SCRIPT1="$SCRIPTS_DIR/merlin-ovpn-client-watchdog.sh"
- SCRIPT2="$SCRIPTS_DIR/services-start"
- mkdir -p $SCRIPTS_DIR
- # -------------------- begin merlin-ovpn-client-watchdog --------------------- #
- cat << 'EOF' > $SCRIPT1
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # time (in secs) between checks for failed/stopped/unresponsive openvpn clients
- INTERVAL=60
- # internet host used for ping checks
- PING_HOST='8.8.8.8'
- # time (in secs) between ping checks
- PING_INTERVAL=10
- # maxmium number of ping checks before being considered a failure
- PING_MAXTRY=3 # (3 recommended, 0 disables ping checks)
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- # function _ping( vpn-network-interface )
- _ping() {
- [ $PING_MAXTRY -gt 0 ] || return 0
- local i=1
- # it's best to check multiple times to prevent false negatives
- while :; do
- ping -qc1 -W3 -I $1 $PING_HOST &>/dev/null && return 0
- [ $(( i++ )) -ge $PING_MAXTRY ] && break || sleep $PING_INTERVAL
- done
- return 1
- }
- # wait for *reliable* internet connection
- until ping -qc1 -W3 $PING_HOST &>/dev/null; do sleep 10; done
- while sleep $INTERVAL; do
- for i in 1 2 3 4 5; do
- # only enabled openvpn clients need to be considered
- [ "$(nvram get vpn_client${i}_state)" != '0' ] || continue
- # check for failed connection or unresponsive tunnel
- pidof vpnclient${i} &>/dev/null && _ping tun1${i} && continue
- # fall-through means failure; restart the openvpn client
- service restart_vpnclient${i} >/dev/null
- echo "openvpn client #$i (re)started @ $(date)"
- done
- done
- } 2>&1 | logger -t $(basename $0 .sh)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT1
- chmod +x $SCRIPT1
- echo "installed: $SCRIPT1"
- # --------------------- end merlin-ovpn-client-watchdog ---------------------- #
- # --------------------------- begin services-start --------------------------- #
- create_script() {
- cat << 'EOF' > $SCRIPT2
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- nohup $SCRIPT1 &>/dev/null &
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT2
- sed "s:\$SCRIPT1:$SCRIPT1:g" -i $SCRIPT2
- chmod +x $SCRIPT2
- }
- if [ -f $SCRIPT2 ]; then
- echo "error: $SCRIPT2 already exists; requires manual installation"
- else
- create_script
- echo "installed: $SCRIPT2"
- fi
- # ---------------------------- end services-start ---------------------------- #
Add Comment
Please, Sign In to add comment