Skip to content

Instantly share code, notes, and snippets.

@soar
Created Mar 29, 2015
Embed
What would you like to do?
Windows VPN auto routes
schtasks /create /F /RL HIGHEST /TN "Маршруты для VPN" /TR "C:\vpnroutes.bat /connect" /SC ONEVENT /EC System /MO "*[System[Provider[@Name='Rasman'] and (Level=4 or Level=0) and (EventID=20267)]] and *[EventData[Data='My VPN Connection']]"
schtasks /create /F /RL HIGHEST /TN "Маршруты для VPN (удаление)" /TR "C:\vpnroutes.bat /disconnect" /SC ONEVENT /EC System /MO "*[System[Provider[@Name='Rasman'] and (Level=4 or Level=0) and (EventID=20268)]] and *[EventData[Data='My VPN Connection']]"
@echo off
rem ===========================================================================
rem Маршруты, которые будут добавлены при отключении VPN.
rem Если установлен маршрут по-умолчанию - обычно не нужны.
rem Формат: IP маска шлюз
rem route novpn: 10.0.0.0 255.0.0.0 172.16.0.1
rem Маршруты, которые будут добавлены при подключении VPN.
rem VPNIP заменяется на текущий адрес интерфейса.
rem формат: IP маска шлюз
rem route vpn: 10.0.0.0 255.0.0.0 VPNIP
rem route vpn: 192.168.0.0 255.255.0.0 VPNIP
rem Имя VPN-подключения
set vpnname=My VPN Connection
rem ===========================================================================
setlocal EnableDelayedExpansion
@set gw=
for /F "tokens=2" %%i in ('netsh interface ipv4 show addresses "%vpnname%" ^| findstr IP') do @set vpnip=%%i
if [%1]==[/connect] goto connect
if [%1]==[/disconnect] goto disconnect
echo Please select /connect or /disconnect option key!
rem pause
goto end
:connect
if "%vpnip%"=="" goto noip
for /F "tokens=4,5,6" %%i in ('type %0 ^| findstr "^rem.*route"') do route delete %%i mask %%j
for /F "tokens=4,5,6" %%i in ('type %0 ^| findstr "^rem.*route.*vpn"') do (
set gw=%gw%%%k
set gw=!gw:VPNIP=%vpnip%!
route add %%i mask %%j !gw!
)
goto end
:disconnect
for /F "tokens=4,5,6" %%i in ('type %0 ^| findstr "^rem.*route"') do route delete %%i mask %%j
for /F "tokens=4,5,6" %%i in ('type %0 ^| findstr "^rem.*route.*novpn"') do route add %%i mask %%j %%k
goto end
:noip
echo Can't set gateway IP, check your settings
goto end
:end
endlocal
rem end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment