1

現在の形式のスクリプト

@echo on

setlocal EnableDelayedExpansion

set /p ipAddress="enter ip address: "

rem right now the loop is set to (1,1,50) for the sake of testing

for /l %%i in (1,1,50) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul

if !errorlevel! == 0 (
deploy_mir.bat %ipAddress%.%%i

)
)

endlocal

次に、オンラインであることがわかっているホスト(10.167.194.22)で実行した結果は

C:\DOCUME~1\socuser2\MIR>test.bat

C:\DOCUME~1\socuser2\MIR>setlocal EnableDelayedExpansion

C:\DOCUME~1\socuser2\MIR>set /p ipAddress="enter ip address: "
enter ip address: 10.167.194

C:\DOCUME~1\socuser2\MIR>for /L %i in (22 1 50) do (
ping -n 1 10.167.194.%i   | find "TTL"  1>nul
if !errorlevel! == 0 (deploy_mir.bat 10.167.194.%i )
)

C:\DOCUME~1\socuser2\MIR>(
ping -n 1 10.167.194.22   | find "TTL"  1>nul
if !errorlevel! == 0 (deploy_mir.bat 10.167.194.22 )
)
"Mir Agent deployment to: 10.167.194.22"

最後の行は、!errorlevel! を意味します。== 0 (つまり、「TTL」が実際に見つかった) したがって、この時点まではスクリプトが機能しているように見えます。ただし、次のループでは、 10.167.194.23 (生きている) と .30 および .46 がスキップされました。追加することにしました

echo %errorlevel% 

ループの最後で、ここで何が起こっているかを確認してください。どうやら、すべての ping の後 %errorlevel% が明らかに 0 だったようです

ping -n %ipAddress%.%%i | find "TTL" >nul

私の問題があるところです。このステートメントによると、10.167.194.22-.50 の間で 3 台のマシンのみが稼働しているすべての ping の後に "TTL" が検出されていますが、これは誤りです。

ところで、私がそうするとき

!errorlevel! == 0

どういう意味ですか?

この線より下はすべて 2012 年 4 月 26 日現在のものです。

So my new script looks like this 

@echo on


set /p ipAddress="enter ip address: "


set i=
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i> test.txt
find "TTL" test.txt
if %errorlevel% == 0 (
deploy_this.bat %ipaddress%.%%i
)

最初に if errorlevel チェックなしでスクリプトを試してみましたが、うまくいきました。私が提供したIPアドレスへのpingを開始し、.2 .3 .4 .5などに進みました。

これを追加したら...

if %errorlevel% == 0 (
deploy_this.bat %ipaddress%.%%i
)

これが起こることです

C:\DOCUME~1\socuser2\MIR>test.bat
C:\DOCUME~1\socuser2\MIR>set /p ipAddress="enter ip address: "
enter ip address: 10.167.201
C:\DOCUME~1\socuser2\MIR>set i=
C:\DOCUME~1\socuser2\MIR>

そして、スクリプトは停止します。何か案は?

4

3 に答える 3

2

いくつかの問題があります:

  • callステートメントを使用しない限り、バッチファイルは返されません
  • 括弧がありません
  • FORループ内では、使用する必要がenabledelayedexpansionあります!ERRROLEVEL!

このコードを見ることをお勧めします。いくつかの改善点があります。

  • setlocal を使用して、変数をそれ自体に保持します
  • 一時ファイルを生成しません
  • コマンドラインまたはプロンプトからIPアドレスを取得できます
  • 凹んでいます
  • 出力は冗長ではありません

ここにあります :

@echo off

setlocal EnableDelayedExpansion

set ipAddress=%1

if "%ipAddress%"=="" SET /P ipAddress="enter ip address: "

for /l %%i in (1,1,2) do (

    rem Remove the > nul at the end if you want to 
    rem see the ping results in the output
    ping -n 1 %ipAddress%.%%i | find "TTL" > nul

    if !ERRORLEVEL! == 0 (
       call deploy_this.bat %ipAddress%.%%i
    )
)

endlocal
于 2012-04-25T18:57:35.190 に答える
0

おそらく私はあなたのループを次のように書き直します:

FOR /L %%i IN (1,1,50) DO (
  ping -n 1 %ipAddress%.%%i | find "TTL" >NUL && (
    CALL deploy_mir.bat %ipAddress%.%%i
  )
)
于 2012-04-27T14:33:13.170 に答える
0

言語に依存しない ping 応答分析は次のようになります。

  ping -n 1 %ipAddress%.%%i
  if errorlevel 1 goto :dead
于 2012-07-23T06:21:24.117 に答える