Script to ping through tun0 and restart openvpn if connection lost

hi everyone :slight_smile:
it's that simple .. I live in somewhere that I always need vpn to do anything
my isp keep disconnecting every hour or so
all what I need is a script that keep pinging google every 5s
and if the connecting is lost just restart openvpn service after 30s
thanks for your time

Something like this Smoothly restarting wireless radio, travelmate and openvpn? - #13 by jeff1

1 Like

pls can u explain the code for me ?
I want the to give 3 minutes for the first time to make sure the router is up and running
and then I want to ping every 5s and if the router lost the connection it will restart openvpn after 30s

#!/bin/sh
n=10
while sleep 50; do
        t=$(ping -c $n 8.8.8.8 | grep -o -E '\d+ packets r' | grep -o -E '\d+')
        if [ "$t" -eq 0 ]; then
                /etc/init.d/openvpn restart
        fi
done

Googled Linux shell scripting?

no .. because I'm exhausted after working all day long and returned to my home just wanted to do this little thing to my router befor going to sleep ..
anyway thanks for helping