Page 1 of 1
Run script on connection fail
Posted: Sat Nov 16, 2013 1:25 pm
by niawag
Hi all, I'd like to be able to run a script when my VPN connection fails. I'm already able to run scripts when the connection is up or down (with up and down command) but it is not working if the connection is not established at all.
My goal is to be able to keep a VPN connection alive without interacting with it. As of now I can kill and restart openvpn if the connection goes down, I can also make it connect to another server. But if the initial connection fails I'm stuck. Is there a command tthat allws to run a script in case the connection fails?
Thanks for your help!
Re: Run script on connection fail
Posted: Sun Nov 17, 2013 4:54 pm
by rainbow6
You can create a startup script that will start the service and if fail run another script. Your script can be something along the lines:
Code: Select all
service openvpn start
if ps ax | grep -v grep | grep openvpn > /dev/null
then
echo "openvpn service is running, everything is fine"
else
echo "Service is down" | mail -s "OpenVPN is down" youremail@address.com
fi
You get the idea.
Re: Run script on connection fail
Posted: Mon Nov 18, 2013 4:10 pm
by niawag
Hi and thanks for your answer, I'm not on linux and I'm not running OpenVPN as a service but I didn't thought of that way to do it. I'll try and see what I can do on windows. Thanks for your help!
Re: Run script on connection fail
Posted: Tue Nov 19, 2013 3:02 pm
by niawag
Hi again, I found another way to deal with my problem, as it may help other users I decided to post it here.
The first script is ran when the VPN goes UP or DOWN :
VPN_up_down.bat
Code: Select all
@echo off
set vpn=%config%
set vpn=%vpn:.ovpn=%
cd "C:\Program Files (x86)\OpenVPN\config\logs"
echo %date% %time:~0,8% - %vpn% %script_type% >> reco_log.txt
REM we try to reconnect only if the disconnection is not coming from the user to prevent unstoppable program
if "%signal%" == "sigterm" exit
setlocal ENABLEDELAYEDEXPANSION
if "%script_type%" == "down" (
REM if vpn is down, we try with another server
set /p vpnserver=<vpnserverlist.txt
more +1<vpnserverlist.txt>temp
echo !vpnserver!>>temp
move temp vpnserverlist.txt
taskkill /F /IM openvpn-gui.exe
taskkill /F /IM openvpn.exe
del "C:\Program Files (x86)\OpenVPN\log\*.log"
start "" "C:\Program Files (x86)\OpenVPN\bin\openvpn-gui.exe" --connect !vpnserver! --silent_connection 1 --show_balloon 0
echo %date% %time:~0,8% - reco !vpnserver! >> reco_log.txt
REM and we check if it is connected
cd "C:\Program Files (x86)\OpenVPN\config\scripts\"
set vpn_UP=!vpnserver!
set vpn_DOWN=%vpn%
start /B "" "VPNCheckUP.bat"
)
and this second script looks into the log file to see if the connection is ok, if it's not it tries to connect to another server :
VPNCheckUP.bat
Code: Select all
@echo off
set vpn=%vpn_UP%
set vpn=%vpn:.ovpn =%
ping -n 60 127.0.0.1>NUL
cd "C:\Program Files (x86)\OpenVPN\log"
copy %vpn%.log tmp.log
findstr "Initialization Sequence Completed" tmp.log
if %errorlevel%==0 (
REM if the VPN connected we log it
cd "C:\Program Files (x86)\OpenVPN\config\logs"
echo %date% %time:~0,8% - %vpn% Running >> reco_log.txt
exit
) else (
REM else (VPN not connected) we run it with another server
cd "C:\Program Files (x86)\OpenVPN\config\logs"
echo %date% %time:~0,8% - %vpn% Not Running >> reco_log.txt
setlocal ENABLEDELAYEDEXPANSION
set /p vpnserver=<vpnserverlist.txt
more +1<vpnserverlist.txt>temp
echo !vpnserver!>>temp
move temp vpnserverlist.txt
taskkill /F /IM openvpn-gui.exe
taskkill /F /IM openvpn.exe
del "C:\Program Files (x86)\OpenVPN\log\*.log"
start "" "C:\Program Files (x86)\OpenVPN\bin\openvpn-gui.exe" --connect !vpnserver! --silent_connection 1 --show_balloon 0
echo %date% %time:~0,8% - reco !vpnserver! >> reco_log.txt
REM And we check if it is connected
cd "C:\Program Files (x86)\OpenVPN\config\scripts\"
set vpn_UP=!vpnserver!
set vpn_DOWN=%vpn%
start /B "" "VPNCheckUP.bat"
)
The file vpnserverlist.txt contains a list of ovpn files:
server1.ovpn
server2.ovpn
...
Hope this will help others!
Note : I'm no script dev so feel free to (upgrade/correct/adapt to your need) my proposition