0

我正在编写一个 bat 脚本,用于自动创建 /32 路由到可通过 LAB 隧道接口访问的网络中的设备。

我必须这样做,因为 LAB 隧道必须设置在另一个(公司隧道)上,如果没有到达目的地的路由,它会自动转发其中的数据包。因此,我为网络中的设备创建了所有 /32 路由,以防止转发公司隧道。

以下脚本可以解决问题,但由于我不知道的原因,我必须运行它 3 或 4 次才能工作。(注意我是一个有 bat 脚本的大菜鸟)

@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

通常,这是产生的输出:

[...]>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

如您所见,在运行此脚本 3 次后,它终于可以工作了。您能否帮助确定导致此问题的原因?

我提前感谢您非常感谢您的支持。

此致,

西尔万。

4

2 回答 2

1

这是最终脚本(如果有人感兴趣):

@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

再次感谢你的帮助 !

最好的问候,西尔万

于 2011-11-24T11:08:27.067 回答
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.

于 2011-11-23T21:17:48.357 回答