Help with VPN monitor Daemon Script

Discussion in 'General Discussion' started by Hardrock, Dec 23, 2017.

  1. Hardrock

    Hardrock Networkin' Nut Member

    Hi all, I'm trying to get this script to work without success. The idea is to make a daemon that compares my GW IP address with my Real world IP address (assuming the VPN is up). If the two are the same, it forces Openvpn to start, otherwise it ignores it.

    There is probably a more elegant way of doing this, so any help appreciated to fix it. I'd like to place the script in the Admin > WAN up part of Tomato. Thank you.

    #!/bin/sh
    while :
    do
    sleep 60
    var=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
    echo $var
    myip=$(curl icanhazip.com)
    echo $myip
    if [ "$myip" == "$var" ]
    then
    echo " openvpn not running, i will start it"
    openvpn --config 'vpn.ovpn' --daemon
    else
    echo " openvpn running fine, sleeping"
    fi
    done
     
  2. skameykin22

    skameykin22 New Member Member

    And how did you solved the problem?
     
  3. Hardrock

    Hardrock Networkin' Nut Member

    Hi Skameykin22 and anyone else following this thread. Here is a solution that works 100%. You will see in the script i no longer use a Daemon, instead I am benefiting from the built in scheduler in Tomato (cronjob). The scheduler is setup to run the script every miniute. Simply paste the script into the custom scheduler, located in Administration in Tomato.

    The script below does three things:

    1) It tests whether a VPN Tunnel is established. In my case this will be tun0. If tun0 is not detected, it will attempt to restart the VPN. Do an ifconfig when your VPN is up to determine your tun(X).

    2) It will ping google DNS servers 8 times. (4 x 8.8.8.8 and 4 x 4.2.2.4). To be successful, only one ping needs to be satisfactory. You may reduce the count if required. If No pings are received, the script will attempt to restart the VPN.

    3) The script logs both successful and failed VPN events. This is Ideal if you want to test your script works, or measure drop-outs from your VPN service provider.

    Give the script a go and let me know how you get on!

    Cheers John

    **************************************************************

    #!/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    HOSTS="8.8.8.8 4.2.2.4"
    totalcount=0
    COUNT=4

    DATE=`date +%d-%m-%Y:%H:%M:%S`

    if ! ifconfig tun0 | grep -q "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00"
    then
    echo "$DATE openvpn not running...service restarted." >> /tmp/VpnOff.log
    sleep 1
    openvpn --config vpn.ovpn & #point this to your vpn config line
    else
    for myHost in $HOSTS;
    do
    count=`ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'`
    totalcount=$(($totalcount + $count))
    done

    if [ $totalcount -eq 0 ]
    then
    echo "$DATE openvpn not running...service restarted." >> /tmp/VpnOn.log
    sleep 1
    openvpn --config vpn.ovpn & #point this to your vpn config line
    else
    sleep 1
    echo "$DATE vpn ok. $totalcount ping checks passed" >> /tmp/VpnOn.log

    fi
    fi
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice