Run script on connection fail

How to customize and extend your OpenVPN installation.

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

Post Reply
niawag
OpenVpn Newbie
Posts: 4
Joined: Sat Nov 16, 2013 1:16 pm

Run script on connection fail

Post by niawag » Sat Nov 16, 2013 1:25 pm

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!

rainbow6
OpenVPN Power User
Posts: 54
Joined: Mon Aug 29, 2011 1:01 pm

Re: Run script on connection fail

Post by rainbow6 » Sun Nov 17, 2013 4:54 pm

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.

niawag
OpenVpn Newbie
Posts: 4
Joined: Sat Nov 16, 2013 1:16 pm

Re: Run script on connection fail

Post by niawag » Mon Nov 18, 2013 4:10 pm

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!

niawag
OpenVpn Newbie
Posts: 4
Joined: Sat Nov 16, 2013 1:16 pm

Re: Run script on connection fail

Post by niawag » Tue Nov 19, 2013 3:02 pm

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

Post Reply