Skip to content
Conditional Connection batch script
Got a problem with Viscosity or need help? Ask here!
I thought someone else might find my batch script useful, as I didn't find this example provided elsewhere on this site.
I lent on this example for Mac OSX: http://www.sparklabs.com/forum/viewtopi ... onal#p3404
This batch script checks the subnet of your current IP address, and compares it to the IP address of your "office". If you have the same subnet, then we assume you are in the office (rather than at home), and so you don't want the VPN connected.
Change the CONSTANTS to suit. In Viscosity, load the script as a "Before Connect" script on the Advanced tab.
Cheers
Sam
I lent on this example for Mac OSX: http://www.sparklabs.com/forum/viewtopi ... onal#p3404
This batch script checks the subnet of your current IP address, and compares it to the IP address of your "office". If you have the same subnet, then we assume you are in the office (rather than at home), and so you don't want the VPN connected.
Change the CONSTANTS to suit. In Viscosity, load the script as a "Before Connect" script on the Advanced tab.
Code: Select all
EDIT (7 Oct 2014): Updated script with "ViscosityNoConnect" signal as suggested by Eric (see below).@echo off
rem Batch script: VPN conditional connection
rem If connected to office subnet, do not connect to VPN.
rem CONSTANTS
rem subnet=<first-3-octets> eg 192.168.2
set OFFICE_SUBNET=192.168.2
set VISCOSITYCC="C:\Program Files\Viscosity\ViscosityCC.exe"
set CONN_NUM=1
rem VARIABLES
set conn_name=
set IP=
set myIP=
set current_subnet=
rem EXECUTE
rem Get the 1st VPN's name
for /f "delims=" %%a in ('"%VISCOSITYCC% getname %CONN_NUM%"') do set "conn_name=%%a"
rem Get my main IP address
for /f "tokens=2,3" %%a in ('ping %computername% -n 1 -4') do if "from"== "%%a" set "IP=%%~b"
set myIP=%IP:~0,-1%
rem Get just first 3 octets of IP address
for /f "tokens=1,2,3 delims=." %%a in ("%myIP%") do set "current_subnet=%%a.%%b.%%c"
rem Conditional connection: check if current subnet is office subnet
if "%current_subnet%" == "%OFFICE_SUBNET%" (
echo ViscosityNoConnect
)
rem else continue with the connection
Cheers
Sam
Last edited by samsplan on Tue Oct 07, 2014 10:08 pm, edited 2 times in total.
Hi samsplan,
Thanks for the contribution! Viscosity will keep an eye out though for a value of ViscosityNoConnect coming from the BeforeConnectScript, so instead of telling another Viscosity instance to connect or disconnect, you can simply "echo ViscosityNoConnect" to tell Viscosity to cancel the connection attempt. For example, you could change your last couple of lines too:
if "%current_subnet%" == "%OFFICE_SUBNET%" (
echo ViscosityNoConnect
) else (
REM Continue with the connection
)
Thank you again for your contribution though, we highly appreciate it and hope some people find it helpful!
Regards,
Eric
Thanks for the contribution! Viscosity will keep an eye out though for a value of ViscosityNoConnect coming from the BeforeConnectScript, so instead of telling another Viscosity instance to connect or disconnect, you can simply "echo ViscosityNoConnect" to tell Viscosity to cancel the connection attempt. For example, you could change your last couple of lines too:
if "%current_subnet%" == "%OFFICE_SUBNET%" (
echo ViscosityNoConnect
) else (
REM Continue with the connection
)
Thank you again for your contribution though, we highly appreciate it and hope some people find it helpful!
Regards,
Eric
Eric Thorpe
Viscosity Developer
Web: http://www.sparklabs.com
Support: http://www.sparklabs.com/support
Twitter: http://twitter.com/sparklabs
Viscosity Developer
Web: http://www.sparklabs.com
Support: http://www.sparklabs.com/support
Twitter: http://twitter.com/sparklabs
3 posts
Page 1 of 1