4

我敢打赌,这很简单,而且我感觉由于嵌套循环,它无法按我需要的方式工作。这是一个简单的脚本,我在某个地方完成了一些修改,以便进行更多测试。从本质上讲,它只是一遍又一遍地延迟主机,直到您关闭它,但记录失败的响应。一切正常,但对于我来说,我无法弄清楚如何让它更新 for 循环中使用的名称。

@echo off
setlocal enabledelayedexpansion

:: ** Enter name to identify host in log **
set name1=Test 1
set name2=Test 2
set name3=Test 3

:: ** Enter IP or HOSTNAME to test against **
set hostIP1=1.1.1.1
set hostIP2=2.2.2.2
set hostIP3=3.3.3.3

:: ** Enter Delay in milliseconds between each test **
set delayMS=2000


title -DO NOT CLOSE- Ping Test
echo DO NOT CLOSE
echo Ping Test


:loop
set namecount=1
for %%x in (%hostIP1% %hostIP2% %hostIP3%) do (
set pingline=1
set pingname=name!namecount!
for /f "delims=" %%A in ('ping -n 1 -w 2000 -l 255 %%x') do (
    if !pingline! equ 2 (
        set logline=!date! !time! "%%A" - %%x !%pingname%!
        echo !logline! | find "TTL=">nul || echo !logline! >> pinglog.txt
        )
    set /a pingline+=1
    )
set /a namecount+=1
)
sleep -m %delayMS%
goto loop

通常我会使用另一种语言,但我需要可以在各种 Windows 服务器上运行的东西。这似乎是最可靠的方法。谢谢

4

1 回答 1

0

这是不正确的:

!%pingname%!

试试这个:

!Pingname!

我无法尝试您的代码,但我做了其他小的更正:

(更新)

 @echo off
setlocal enabledelayedexpansion

:: ** Enter name to identify host in log **
set "name1=Test 1"
set "name2=Test 2"
set "name3=Test 3"

:: ** Enter IP or HOSTNAME to test against **
set "hostIP1=1.1.1.1"
set "hostIP2=2.2.2.2"
set "hostIP3=3.3.3.3"

:: ** Enter Delay in milliseconds between each test **
set /A "delayMS=2000"


title -DO NOT CLOSE- Ping Test
Echo DO NOT CLOSE
Echo Ping Test

:loop
set /A "namecount=1"
for %%x in (%hostIP1% %hostIP2% %hostIP3%) do (
    set /A "pingline=1"
    call set "pingname=%%name!namecount!%%"
    for /f "delims=" %%A in ('ping -n 1 -w 2000 -l 255 %%x') do (
        if !pingline! equ 2 (
            set "logline=%date% !time! %%A - %%x !pingname!"
            (echo !logline! | find "TTL=">nul) || (echo !logline! >> "pinglog.txt")
        )
        set /a "pingline+=1"
    )
    set /a "namecount+=1"
)
sleep -m %delayMS%
goto :loop
于 2012-11-22T17:51:46.760 回答