我知道这是一个旧帖子,但我希望将来可以帮助其他人。下面的脚本将从iplist.txt
文件中获取每个地址,并且pathping
每个地址仅持续 20 秒。然后将每个结果输出到result.txt
文件中。
现在由于pathping
命令的工作方式,“停止”它的唯一方法是终止它自己的 CMD 窗口。由于批处理代码的方式(例如与 C++ 不同),您不能同时运行两行代码(例如,与 C++ 不同),我们可以使用start "" cmd /C ""
命令调用两个新窗口。
对于每个地址,我们召唤一个pathping
窗口和一个taskkill
窗口。taskill 窗口将使用PID #
pathping 窗口的 Task 并等待 20 秒后终止它。
同时,核心批处理文件将等待 40 秒,让所有文件都完成并关闭。然后它将所有数据合并到result.txt
文件中。
@ECHO OFF
@SETLOCAL enableextensions enabledelayedexpansion
@TITLE PathPing from textfile, cancel request for each x after 20s.
@GOTO :Start
:Start
echo WARN: Starting PathPing Script...
echo WARN: This may take some time...
goto :PathPing
:PathPing
:: Large number is for token gathering
set /a number=123456789
for /f "delims== tokens=1,2" %%G in (iplist.txt) do (
:: Start The pathping's for each %%G
start "!number!" cmd /C "pathping "%%G" >> "!number!.outputfile""
:: Start Kill Window (Let process live for 20 seconds)
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "!number!" ^| find "Console"') do set PID=%%#
start "" cmd /C "(PING localhost -n 20 >nul) & (taskkill /pid !PID! /t /f & goto :eof)"
set /a number+=1
)
:: End of FOR statement. Now copy .outputfile to one file.
:: Wait 60 Seconds for the FOR statement to finish copy there files.
PING localhost -n 30 >nul
:: DEL is optional, remove it if you don't with to over-wright text file.
If exist "%~dp0\result.txt" (goto :Del) ELSE (goto :Skip)
:Del
Del /Q "%~dp0\result.txt"
Goto :Skip
:Skip
For %%A In (*.outputfile) DO (for /f "delims== tokens=1,2" %%G in (%%A) do echo %%G >> result.txt)
:: Clean up all .outputfile files.
For %%A In (*.outputfile) DO (DEL /Q "%~dp0\%%A")
Goto :Finish
:Finish
cls
echo WARN: Result is ready.
echo(
pause
goto :eof