LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   auto-reconnect vpn tunnel? (https://www.linuxquestions.org/questions/linux-software-2/auto-reconnect-vpn-tunnel-4175500551/)

tonj 04-04-2014 04:25 AM

auto-reconnect vpn tunnel?
 
I have a centos 6.5 server 64bit running openvpn-2.3.2-2.el6.x86_64 as a client, and it connects to a vpn service on the internet. I use this machine as a proxy server for all my computers and they surf the internet through the vpn tunnel.
The problem is the vpn tunnel goes down at random and I want to know if there's a way auto-reconnect it when it goes down. I've written a little script called reset.sh which always works in re-connecting the vpn tunnel but I have to run it myself. I'd like the server to run this script automatically however the server needs a way to 'know' that the vpn tunnel has gone down and then run the reset.sh script. Is this possible? is there a program that will cater for this?

pingu 04-04-2014 01:21 PM

I don't know of any program to handle this, but you could write a small shell script.
My first thought is you could check the existence of tunnel with "ifconfig" or "ip addr", and greping for an ip in correct range. If that ip doesn't exist then the script calls reset.sh.
Then run that as a cron job - you could set it to run every minute or whatever interval you choose.

tonj 04-07-2014 09:34 AM

so ironic. I've been waiting a few days now for the vpn tunnel to go down so I can inspect the logs, see what they say and try what you said but the connection has been solid.

tonj 04-22-2014 03:10 AM

I found a solution to this that may benefit others who have the same problem....
First you need to make sure openvpn is keeping its own log, which in my case is /home/share/openvpn-client.log.
then...
Install monit.
In the monit control file I have this:
check file openvpn-client.log with path /home/share/openvpn-client.log
if match "RESOLVE: Cannot resolve host address" then exec "/root/reset.sh"


reset.sh is a little script I wrote to reconnect my vpn but a normal user could replace /root/reset.sh with /etc/init.d/openvpn restart

this solution is working fine and it has already reconnected my vpn twice while I was away from the pc. From the time the vpn tunnel goes down it takes about 2 mins for the above process to get it back up again.


All times are GMT -5. The time now is 01:01 PM.