2011-11-23 60 views
0

我正在寫一個蝙蝠腳本自動創建/ 32路由通過實驗室隧道接口可達網絡中的設備。蝙蝠腳本自動添加路由 - 必須運行幾次

我必須這樣做,因爲LAB隧道必須設置在另一個(公司隧道)上,如果目標沒有路由,它會自動轉發其中的數據包。因此,我爲網絡中的設備創建了所有/ 32路由,以防止轉發公司隧道。

下面的腳本可以讓我知道,但是對於我來說,我不得不運行它3到4次。 (注意:我是一個小白蝙蝠腳本)

@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次後,終於工作。 你能幫忙找出是什麼原因造成這個問題?

我非常感謝您的支持。

此致敬禮,

Sylvain。

回答

0

如果你嘗試下面的例子中,你會看到這不起作用:

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

%net%不會產生價值,否則就會有一箇舊的價值。

有關更多信息,請參閱Variable Expansion in FOR Loops爲什麼發生這種情況。

而不是使用setlocal enableextensions你也可以叫subroute來解決這個問題:

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 

爲什麼如果你多次運行它,它的工作原理的原因,就是喜歡set "net=10.%labid%" set命令都得到執行,並淨得到正確的價值。當你第二次運行它時,net仍然具有上一次運行的值,所以它在那一刻將按預期工作。每次運行它時,另一個set=都會得到正確的值。

+1

謝謝你這麼多Wimmel。你讓我弄清楚是什麼原因造成這個問題。我最終根據你的建議更新了腳本,現在它工作正常。 – user1062260

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 

感謝您的幫助!

最好的問候, 維爾託德