从列表中获取主机名然后ping主机名的批处理脚本,如果结果则执行操作

时间:2013-11-25 12:31:34

标签: batch-file

来自:Batch ERRORLEVEL ping response 我发现了这段代码

for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
echo %MATCHES%

我正在尝试将“racer”替换为我从文本文件中获取的变量:

for /f %%x in (computers.txt)

尝试嵌套2 for循环给了我错误

在此之后我想做这样的事情(还没试过这段代码):

    IF "%MATCHES%"=="0" (

shutdown /h /m \\%%x


) ELSE (

IF (

"%MATCHES%"!=="0"

)

echo "ping failed to %%x" >> failed01.txt

)

1 个答案:

答案 0 :(得分:1)

试试这个:

@echo off
setlocal

for /f %%a in (computers.txt) do (
  Call :IsPingable %%a && shutdown /h /m \\%%a || echo Ping failed to %%a >> failed01.txt
)
exit /b

:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b