OPNsense Forum

Archive => 18.1 Legacy Series => Topic started by: Julien on March 15, 2018, 10:32:07 pm

Title: Run a script after VPN up ( Windows )
Post by: Julien on March 15, 2018, 10:32:07 pm
Hi Guys,
Is there is a way to config the OPENVPN to run a script after the tunnel is up ?
the script will be like connect the network drive or run a RDP.

I hope someone got this working as I've been working with this for weeks now and can't get it set up.

Thank you
Title: Re: Run a script after VPN up ( Windows )
Post by: elektroinside on March 15, 2018, 11:08:12 pm
I think you could schedule a task (every 1 min is the lowest if i remember correctly) that runs a powershell or batch script, which verifies if you get an ip address on the OpenVPN interface, confirm that it's working by pinging something on the other side and execute stuff if it does... Just an idea, didn't actually try anything like this.
Title: Re: Run a script after VPN up ( Windows )
Post by: BeNe on March 16, 2018, 08:20:13 am
You can give Viscosity OpenVPN Client a try --> https://www.sparklabs.com/viscosity/features/#experts
Looks like the have the needed feature included:
Quote
Script Everything.
Control Viscosity using AppleScript, or call custom AppleScript or Batch/Vbs scripts when a connection connects or disconnects.
Title: Re: Run a script after VPN up ( Windows )
Post by: elektroinside on March 16, 2018, 09:23:35 am
Unfortunately, Viscosity is a little unstable occasionally... but you could try to combine the two, of course :)
Title: Re: Run a script after VPN up ( Windows )
Post by: Julien on March 16, 2018, 12:22:30 pm
You can give Viscosity OpenVPN Client a try --> https://www.sparklabs.com/viscosity/features/#experts
Looks like the have the needed feature included:
Quote
Script Everything.
Control Viscosity using AppleScript, or call custom AppleScript or Batch/Vbs scripts when a connection connects or disconnects.
Thank you for your answer,
Viscosity is payed version right ?
Title: Re: Run a script after VPN up ( Windows )
Post by: BeNe on March 16, 2018, 05:14:00 pm
Quote
Viscosity is payed version right ?
Yes - 9$ for the lifetime including Updates for the 1.x Versions (if i am right)
Title: Re: Run a script after VPN up ( Windows )
Post by: Julien on March 16, 2018, 06:20:16 pm
Quote
Viscosity is payed version right ?
Yes - 9$ for the lifetime including Updates for the 1.x Versions (if i am right)
i just bought one licenties however its not supporting to run the script.
I've seen somewhere the openvPN does the job.
can't seem to find a working script.

this the last working script I had


Code: [Select]
@echo off
echo.
echo.
ECHO Checking for an Internet connection, please wait...
PING -n 1 www.google.com|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :TRYAGAIN
:TRYAGAIN
echo.
ECHO FAILURE!
ECHO Let me try a bit more, please wait...
echo.
@echo off
PING -n 3 www.google.com|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS2
IF ERRORLEVEL 1 goto :TRYIP
:TRYIP
echo.
ECHO FAILURE!
ECHO Checking DNS...
ECHO Lets try by IP address...
echo.
@echo off
ping -n 1 8.8.8.8|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESSDNS
IF ERRORLEVEL 1 goto :TRYROUTER
:TRYROUTER
echo.
ECHO FAILURE!
ECHO Lets try pinging the router....
echo.
ping -n 2 192.168.1.1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :ROUTERSUCCESS
IF ERRORLEVEL 1 goto :NETDOWN
:ROUTERSUCCESS
echo.
ECHO It appears that you can reach the router, but internet is unreachable.
echo.
goto :FAILURE
:NETDOWN
echo.
ECHO FAILURE!
ECHO It appears that you having network issues, the router cannot be reached.
echo.
goto :FAILURE
:SUCCESSDNS
echo.
ECHO It appears that you are having DNS issues.
echo.
goto :FAILURE
:SUCCESS
echo.
ECHO You have an active Internet connection
echo.
goto start
:SUCCESS2
ECHO You have an active internet connection but some packet loss was detected.
pause
goto start
:FAILURE
cls
echo.
echo.
ECHO You do not have an active Internet connection
echo Please check your WiFi Connection and try again?
echo.
pause
exit
:start
REM Try to ping a local server inside my network
echo Checking to see if your connected via VPN or at the Office...
PING -n 1 192.168.168.2|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :CHECKDEPT
IF ERRORLEVEL 1 goto :STARTVPN
:STARTVPN
echo Checking to see if VPN is already loaded...
echo.
tasklist /FI "IMAGENAME eq openvpn.exe" | find /i "openvpn.exe"
IF ERRORLEVEL 2 GOTO VPNEXIST
IF ERRORLEVEL 1 GOTO VPNNOEXIST

:VPNEXIST
goto EXIT

:VPNNOEXIST
goto VPNNOTCONNECTED

:EXIT
tasklist /FI "IMAGENAME eq openvpn.exe" | find /i "openvpn.exe"
IF ERRORLEVEL 2 GOTO VPNCONNECTED
IF ERRORLEVEL 1 GOTO VPNNOTCONNECTED
:VPNCONNECTED
GOTO checkdept
:VPNNOTCONNECTED
taskkill /f /im openvpn.exe
taskkill /f /im openvpn-gui.exe
cls
echo.
echo.
echo Waiting for VPN to reset...
CHOICE /C X /T 4 /D X > NUL
start openvpn-gui.exe --connect clockmedical.ovpn
ECHO.
ECHO.
CHOICE /C X /T 2 /D X > NUL
goto :checkdept
:CHECKDEPT
cls
echo.
echo Waiting for a solid VPN connection... Please Wait.
PING -n 1 192.168.168.11|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :END
IF ERRORLEVEL 1 goto :CHECKDEPT
:END
REM This starts Remote Desktop called RemoteDesktop.rdp from the users profile desktop
cd\users\%username%\desktop
echo.
start mstsc RemoteDesktop.rdp
Title: Re: Run a script after VPN up ( Windows )
Post by: elektroinside on March 16, 2018, 07:09:58 pm
Yes it does.. not to load one inside viscosity, but use the command line version and get feedback from it to act upon.
https://www.sparklabs.com/support/kb/article/controlling-viscosity-with-scripting-windows/
Title: Re: Run a script after VPN up ( Windows )
Post by: bigops on March 17, 2018, 01:01:03 am
You might want to look at this post.  This looks like what you are trying to achieve. 

https://www.raymond.cc/blog/create-an-event-in-event-viewer/

https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/
Title: Re: Run a script after VPN up ( Windows )
Post by: elektroinside on March 17, 2018, 06:20:09 am
Or take a look at this:

http://www.sparklabs.com/support/kb/article/running-batch-vbs-scripts-when-connected-disconnected/

If you would like to use the GUI, that can run batch and vbs scripts. You can combine everything to get exactly what you want.
Title: Re: Run a script after VPN up ( Windows )
Post by: skymaster on March 17, 2018, 08:54:51 am
Hi there,

maybe you wanne give the free Securepoint-VPN Client a chance ;)

The description looks promising.

https://www.securepoint.de/produkte/utm-firewalls/vpn-client.html

Cheers