0

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.

user1062260
  • 11
  • 1
  • 1
  • 3

2 Answers2

1

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

user1062260
  • 11
  • 1
  • 1
  • 3
0

If you try the following example, you will see this does not work:

@echo off
set labid=104
if %labid% GTR 98 ( 
    if %labid% LSS 255 (
        set "net=10.%labid%"
        echo net=%net%
    )
)
pause

%net% will not have a value, or it will have an old value.

See Variable Expansion in FOR Loops for more information why this happens.

Instead of using setlocal enableextensions you can also call a subroute to solve this problem:

set labid=104
if %labid% GTR 98 ( 
    if %labid% LSS 255 (
        call :setnet
    )
)

pause
goto :eof

:setnet

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

:eof

The reason why it works if you run it multiple times, is that the set command like set "net=10.%labid%" do get executed, and net gets the correct value. When you run it the second time, net still has the value from the previous run, so it will work as expected at that moment. Each time you run it, another set= will get a correct value.

wimh
  • 15,072
  • 6
  • 47
  • 98
  • 1
    Thank you so much Wimmel. You made me figure out what causes this issue. I finally updated script according to your advises and now it works fine. – user1062260 Nov 24 '11 at 11:04