Bat script for automatically adding routes - Must be run several times

user1062260 picture user1062260 · Nov 23, 2011 · Viewed 11.1k times · Source

I'm writing a bat script for automatically creating /32 routes towards devices in a network reachable trough a LAB tunnel interface.

I have to do this because the LAB tunnel has to be set upon an other one (corporate tunnel) that automatically forwards packets inside of it if there is no routes for the destination. Consequently I create all /32 routes for devices in the network in order to prevent forwarding the corporate tunnel.

Following script makes the trick but for an unknown reason to me, I have to run it 3 or 4 times before it works. (Note I'm a big noob with bat script)

@echo off
c:
cd %systemroot%
set /P input=Please enter a LAB ID:
set /A labid=%input%
if %labid% GTR 98 ( 
    if %labid% LSS 255 (
        set "net=10.%labid%"
        for /f "tokens=1-5 delims= " %%A in ('route print ^| findstr %net%') do (
            echo Adding static routes for LAB %labid%...
            set gatewayssl=%%C
            echo Gateway is SSL interface: %gatewayssl%
            for /l %%h in (1,1,254) do call :add_route %%h %gatewayssl%
            goto:EOF
        )
        goto:EOF
    )else (
        echo Invalid Lab ID
        goto:EOF
    )
) else (
    echo Invalid Lab ID
        goto:EOF
)

:add_route
set ipaddr=%net%.0.%1
route add %ipaddr% mask 255.255.255.255 %2% metric 1
goto:EOF

Typically, here is the ouput produced:

[...]>Z:\ALU\SGCC\LAB\labrouting.bat
Please enter a LAB ID:104
FINDSTR : Ligne de commande erronée

C:\WINNT>Z:\ALU\SGCC\LAB\labrouting.bat
Please enter a LAB ID:104
Adding static routes for LAB 104...
Gateway is SSL interface:

Manipule les tables de routage du réseau.

ROUTE [-f] [-p] [cmde [destin]
[route manual apperas many times because of the for loop...]

C:\WINNT>Z:\ALU\SGCC\LAB\labrouting.bat
Please enter a LAB ID:104
Adding static routes for LAB 104...
Gateway is SSL interface: 192.168.104.1

As you can, after running this script 3 times, it finally works. Could you please help to identify what causes this issue ?

I thank you in advance for your very appreciated support.

Best regards,

Sylvain.

Answer

user1062260 picture user1062260 · Nov 24, 2011

Here is the final script (if someone is interested):

@echo off

set /P input=Please enter a LAB ID:
set /A labid=%input%
if /i %labid% lss 98 (goto :eof)
if /i %labid% gtr 255 (goto :eof)

call :sub_set_net
echo Your LAB network is: %net%.0.0

call :sub_get_lab_gw
echo Your LAB gateway is: %gatewayssl%

call :sub_add_lab_routes
goto :eof

:sub_error
echo Invalid LAB id (98<labid<255)
goto :eof

:sub_set_net
set "net=10.%labid%"
goto :eof

:sub_get_lab_gw
route print | findstr %net% > %temp%\TMPROUTINGLAB.txt
for /f "tokens=1-5 delims= " %%A in (%temp%\TMPROUTINGLAB.txt) do set gatewayssl=%%C
del %temp%\TMPROUTINGLAB.txt
goto :eof

:sub_add_lab_routes
echo Adding static routes for LAB %labid%...
for /l %%h in (1,1,254) do call :sub_add_route %%h %gatewayssl%
echo Done
pause
goto :eof

:sub_add_route
set ipaddr=%net%.0.%1
route add %ipaddr% mask 255.255.255.255 %2% metric 1
goto :eof

:eof

Thanks again for your help !

Best regards, Sylvain