Posted on March 9 2023 under networking, windows, and batch
@echo off
rem #################
rem # Configuration #
rem #################
rem Interface name to search for
set interface=SEARCH STRING
rem Routes in the form "<network>/<mask> <gateway>"
set routes[0]=10.0.0.0/8 1.1.1.1
set routes[1]=172.16.0.0/12 1.1.1.1
set routes[2]=192.168.0.0/16 1.1.1.1
set routes[3]=fc00::/7 ::1
rem Route metric
set metric=10
rem #################################
rem # Do not modify below this line #
rem #################################
rem Get the interface ID
for /f "usebackq delims=" %%a in (`route print ^| findstr /c:"%interface%"`) do (
rem Strip leading whitespace
for /f "tokens=* delims= " %%b in ("%%a") do (
rem The interface ID is the first token, followed by periods
for /f "tokens=1 delims=." %%c in ("%%b") do set if=%%c
)
)
rem Set the routes
for /l %%i in (0,1,5) do (
setlocal EnableDelayedExpansion
echo route add !routes[%%i]! metric %metric% if %if%
route add !routes[%%i]! metric %metric% if %if%
echo/
if %ErrorLevel% neq 0 endlocal & goto Error
endlocal
)
echo Done
exit /b 0
:Error
echo Error
exit /b 1