Page 1 of 1

OpenVPN restart script

Posted: Thu Sep 09, 2010 11:45 am
by Admire
Hi,

I have the following situation. 2 lines - main and backup. 2 default gateways - backup with larger metrics. It works well, but I have an OpenVPN client which does not restart when changing from main to backup line. So I basically need script to watch if the backup is on then to restart the OpenVPN interface and wait for the main line to become available again then restart the OpenVPN again. Can anyone give me an example or something?

Re: OpenVPN restart script

Posted: Thu Sep 09, 2010 11:46 am
by mrz
You can also adjust OVPN keepalive timeouts.

Re: OpenVPN restart script

Posted: Thu Sep 09, 2010 11:52 am
by Admire
I tried the idle timeout setting, but the timeout timer resets everytime someone tries to push traffic through the vpn, so it's no use. And I don't want the vpn to disconnect when there is no need

Re: OpenVPN restart script

Posted: Thu Sep 09, 2010 12:00 pm
by mrz
well then you can simply check gateway status and disable/enable ovpn interface

Add comments to routes for example "main" and "backup"

{
:if ([/ip route get [find comment="backup"] active] != false) do={
:local id [/interface ovpn-client find name="ovpn-name"];
/interface ovpn-client disable $id;
/interface ovpn-client enable $id;
}

Re: OpenVPN restart script

Posted: Thu Sep 09, 2010 12:24 pm
by Admire
Thanks, I think that will work.
I'll write 2 similar scripts - one to check if backup is on and restart the VPN and one to check if main is on and restart the VPN. The second one will be disabled in scheduler. When the first one executes it will disable itself and enable the second one and vice versa.

Re: OpenVPN restart script

Posted: Thu Sep 09, 2010 12:26 pm
by mrz
Also add some kind of check whether vpn is already restarted once after gateway change otherwise ovpn interface will be disabled/enabled every x interval