[Resolved] Turn VPN on and off automatically
Posted: 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.
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).
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
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
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
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$
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
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