我想写一个批处理文件来测试多个服务器的ping并返回最低的ping。
我已经测试了此代码,但批处理文件不在服务器上运行。我认为这是因为findstr
。
@echo off
setlocal enableextensions disabledelayedexpansion
rem Initialize variables
set "selected="
set "min=99999999"
set serverList= "188.165.254.85" "94.23.206.130" "176.31.117.82" "37.59.51.212" "188.165.214.76" "37.59.56.102" "94.23.8.105"
echo - Pinging ...
rem Enumerate the hosts to check
for %%a in ( %serverList% ) do (
rem Ping the host and retrieve the average roundtrip
for /f "tokens=6 delims== " %%r in ('
ping -n 1 "%%~a" ^| findstr /r /c:"^ .*ms$"
') do for /f "delims=ms" %%t in ("%%r") do (
echo "%%~a" : %%t ms
rem Determine if the current host has a lower rtt
rem if %%t geq min or min is already 0, then we have
rem a division by 0, else a lower rtt has been found
set /a "1/(min/(%%t+1))" && (
set "selected=%%~a"
set "min=%%t"
)
rem Of course this can be done with delayed expansion,
rem just a question of personal preferences
)
) 2>nul
echo(
echo - Selected ...
echo %selected% : %min%
pause