Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # version: 2.1.0, 03-dec-2021, by eibgrad
- # href: https://tinyurl.com/2p9a68ne
- SCRIPTS_DIR='/jffs/scripts'
- SCRIPT="$SCRIPTS_DIR/services-start"
- mkdir -p $SCRIPTS_DIR
- create_script() {
- cat << "EOF" > $SCRIPT
- #!/bin/sh
- set -x # uncomment/comment to enable/disable 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
- while ! 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}
- echo "openvpn client #$i (re)started @ $(date)"
- done
- done
- ) 2>&1 | logger -t $(basename $0)[$$] &
- EOF
- chmod +x $SCRIPT
- }
- if [ -f $SCRIPT ]; then
- echo "error: $SCRIPT already exists; requires manual installation"
- else
- create_script
- echo "installed: $SCRIPT"
- fi
RAW Paste Data
Copied