Windows Batch Script: Exclude Traffic from VPN Tunnel


@echo off

REM Run this script (route_exclude) post-vpn-connect.

REM Add exclude routes. This allows traffic to these network and hosts to go directly and not use the tunnel.

REM Syntax: route_exclude <network1> <mask1> <network2> <mask2> ...<networkN> <maskN>

REM Example-1: route_exclude 10.0.0.0 255.0.0.0

REM Example-2: route_exclude 10.0.0.0 255.0.0.0 192.168.17.0 255.255.255.0

REM Example-3: route_exclude 10.0.0.0 255.0.0.0 192.168.17.0 255.255.255.0 192.168.24.25 255.255.255.255

 

REM Initialize 'DefaultGateway'

set "DefaultGateway="

 

REM Use the route print command and find the DefaultGateway on the endpoint

@For /f "tokens=3" %%* in (

   'route.exe print ^|findstr "\<0.0.0.0\>"'

   ) Do if not defined DefaultGateway Set "DefaultGateway=%%*"

 

REM Use the route add command to add the exclude routes

:add_route

if "%1" =="" goto end

route delete %1

route add %1 mask %2 %DefaultGateway%

shift

shift

goto add_route

:end