1

下面的脚本可以工作,但是在访问离线主机时会减慢很多。

@echo off
rem Setup the output file. This just wipes the pre-existing file from last set of data mining
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime% > output\datamine.txt
**setlocal enabledelayedexpansion
for /f %%a in (hosts.txt) do (
psexec \\\%%a findstr /I ":" "C:\ProgramData\Firewall\event.log" >> output\datamine.txt )
endlocal**

我重写了一个 ping 语句,打算跳过不应答的主机。这是它的肉:

for /f %%a in (hosts.txt) do (<br>
ping -n 1 -w 100 %%a | for /f "skip=1 tokens=3 delims= " %%b in ('findstr bytes') do (set COMPNAME=%%b)
if errorlevel 1 goto FAIL
psexec \\%%a findstr /I ":" "C:\ProgramData\Firewall\event.log" >> output\datamine.txt )
goto END
:FAIL
echo host not answering
:END
endlocal

问题很明显,因为当主机无法回答 ping 时,我正在退出 do 循环。我需要脚本来查看错误级别 1,然后跳到下一个主机。不知道我应该在哪里结束循环。另外,我不太确定我选择的“goto”行。有什么建议吗?

4

2 回答 2

2

您正在退出 for 循环。而不是做 goto 添加一个 else 并将括号移到它后面。

这是我为此编写的一个函数以及如何实现它。

for /f %%a in (hosts.txt) do (
   call :IsPingable %%a && (
   psexec \\%%a findstr /I ":" "C:\ProgramData\Firewall\event.log" >> output\datamine.txt 
   ) || (
     echo host %%a not answering
   )
)   
exit /b

:IsPingable comp
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b
于 2013-11-01T18:11:15.783 回答
0

示例代码:

for /f %%a in (hosts.txt) do ping -n 1 "%%a">nul && call :success "%%a"|| call :fail "%%a"
goto:eof

:success
echo success: %~1
goto:eof

:fail
echo fail: %~1
goto:eof
于 2013-11-01T18:42:03.937 回答