Reconnect your openvpn tunnel if packets get lost

Check if your openvpn tunnel is still active and if it’s not reconnect it with this bash script:


# Check vpn-tunnel "tun0" and ping cz.nic if internet connection work
if  [ "$(ping -I tun0 -q -c 1 -W 1 | grep '100% packet loss' )" != "" ]; then
        logger -t VPN_Reconnect VPN-Tunnel "tun0" has got no internet connectionection -> restart it
        /etc/init.d/openvpn stop
        sleep 3
        /etc/init.d/openvpn start
        logger -t VPN_Reconnect VPN-Tunnel "tun0" is working with internet connection

Thanks to: