[Resolved] Turn VPN on and off automatically

This forum is for general conversation and user-user networking.

Moderators: TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech, TinCanTech

Locked
declan_k
OpenVpn Newbie
Posts: 2
Joined: Tue Jan 28, 2014 3:03 pm

[Resolved] Turn VPN on and off automatically

Post by declan_k » Tue Jan 28, 2014 3:25 pm

Hi
First time poster and a relative noob on Linux, so go easy on me...

I am trying to automate my connection to my vpn proxy such that the vpn connection is turned on at midnight and off at 7:15AM.
In order to automate it, I wrote the following bash script, located at /usr/local/bin/cloak.sh.

Code: Select all

#!/bin/bash

LOG_FILE=/home/declan/log/cloak.log

LogEntry()
    {
        while read data
        do
            echo "$(date "+%Y %m %d %T") ; $data" >>$LOG_FILE 2>&1;
        done
    }

echo "---------------------" | LogEntry

if [ $1 -eq 1 ]
then
    echo "Turning Cloak On" | LogEntry 
    /etc/init.d/openvpn start proxpn.miami | LogEntry
else
    echo "Turning Cloak Off" | LogEntry
    /etc/init.d/openvpn stop | LogEntry
fi

echo "---------------------" | LogEntry
echo " " | LogEntry
My vpn is working and I have confirmed that the script is executable and that it works by checking my IP address before and after calls to the script, turning the VPN on (1 as parameter) or off (0 as parameter).

Code: Select all

declan@mx:~/log$ wget http://ipecho.net/plain -O - -q ; echo
74.196.220.81 <<-- This is my real IP address
declan@mx:~/log$ sudo /usr/local/bin/cloak.sh 1 <<-- Turn the VPN on
declan@mx:~/log$ wget http://ipecho.net/plain -O - -q ; echo
173.0.8.33 <<-- This is my VPN IP address
declan@mx:~/log$ sudo /usr/local/bin/cloak.sh 0 <<-- Turn the VPN off
declan@mx:~/log$ wget http://ipecho.net/plain -O - -q ; echo
74.196.220.81 <<-- Back to my real IP address
declan@mx:~/log$
I add this script to cron, using sudo crontab -e to ensure that sudo is running the script, with the following lines in the crontab

Code: Select all

# Cloak on at midnight, off at 7:15AM
0 0 * * * /usr/local/bin/cloak.sh 1
15 7 * * * /usr/local/bin/cloak.sh 0 
If I look at the contents of the log, /home/declan/log/cloak.log, it shows that the cron job is being executed at the correct time.

The problem is that when I check my IP address after the cron job tries to start the VPN, my real IP address is still being used. My only guess is that somehow the cron job is not being called with sudo rights, but I can't understand why.

Any help would be appreciated. I am also open to any suggestions on alternative approaches, different scripts, etc...

Thanks
Declan
Last edited by debbie10t on Thu Feb 13, 2014 2:48 pm, edited 1 time in total.
Reason: [Resolved / closed]

declan_k
OpenVpn Newbie
Posts: 2
Joined: Tue Jan 28, 2014 3:03 pm

Re: Turn VPN on and off automatically

Post by declan_k » Tue Feb 04, 2014 3:18 pm

Turns out the solution was just to add the following to my crontab

Code: Select all

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Now everything is working as intended.

Locked