Windows 批处理 - 创建一个工具来 ping IP 列表并输出到达服务器的毫秒(时间)最短的三个 IP

Windows Batch - Creating a tool that pings a list of IPs and outputs the three IPs with the lowest ms (time) to reach the server

我正在尝试制作一个工具,它可以 ping 一个 IP 列表并输出到达服务器的 ms(时间)最短的三个 IP。

输入:-由空格分隔的 IP 列表-例如 1.1.1.1 1.1.1.2 1.1.1.3 等

输出:-具有最低 ms 的三个 IP-(按左侧最低 ms 到右侧第三低 ms 的顺序)例如 1.1.2.2 1.2.3.3 1.4.4.4

到目前为止,我有这个用于 ping 部分:

@echo off
set ip=8.8.8.8
for /f "tokens=4 delims=(=" %%a IN ('ping %ip% -n 1 ^|find "Average = "') do echo Result is %%a
pause

此迷你代码将从每个服务器输出 ms ping。

我需要找到每个服务器的ping并进行比较。

谁能帮我解决这一切?如果我不够详细并且没有显示足够的细节或代码,我很抱歉。

@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

:: IPs from http://etherealmind.com/what-is-the-best-ip-address-to-ping-to-test-my-internet-connection/
Set "IPlist=8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 4.2.2.2 141.1.1.1"
For %%A in (%IPlist%) Do (
  Set IP=%%A
  for /f "tokens=6delims==, " %%B in (
    'ping %%A -n 1^|findstr Average'
  ) Do (
    Set PingRes=%%B
    Set /A Avg=100000+!PingRes:ms=!
    Set MS_!Avg!_!IP:.=!=!IP!
  )
)
Echo Show intermediate results
Set MS_1

Set Cnt=0 & Set "NewIPlist="
For /f "tokens=2,4 delims=_=" %%A in ('Set MS_1') Do (
  Set /A Cnt+=1
  if !Cnt! gtr 3 Goto :End
  Set NewIPlist=!NewIPlist! %%B
)
:End
Echo(
Set NewIPlist=%NewIPlist:~1%
Set NewIPlist

尝试了解批处理的作用,有问题再问

示例输出

> Measure-IP.cmd
Show intermediate results
MS_100013_141111=141.1.1.1
MS_100013_20867220220=208.67.220.220
MS_100014_4222=4.2.2.2
MS_100027_20867222222=208.67.222.222
MS_100028_8888=8.8.8.8
MS_100032_8844=8.8.4.4

NewIPlist=141.1.1.1 208.67.220.220 4.2.2.2
@echo off
setlocal EnableDelayedExpansion

rem Set the list of IP's
set "IPs=8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 4.2.2.2 141.1.1.1"

rem Set the number of desired IP's in the output
set "num=3"

rem Initialize "MS" array with large numbers
for /L %%i in (1,1,%num%) do set "MS[%%i]=9999"

for %%a in (%IPs%) do (
   set "ip=%%a"
   for /F "tokens=6 delims== " %%b in ('ping %%a -n 1 ^| find "Average = "') do (
      set "ms=%%b" & set "ms=!ms:ms=!"
      REM ECHO %%a = !ms!

      rem Insert this ms and ip values in their right position in MS and IP arrays
      for /L %%i in (1,1,%num%) do if !ms! lss !MS[%%i]! (
         set /A t=MS[%%i], MS[%%i]=ms, ms=t
         set "t=!IP[%%i]!" & set "IP[%%i]=!ip!" & set "ip=!t!"
      )

  )
)

echo %IP[1]% (%MS[1]%), %IP[2]% (%MS[2]%), %IP[3]% (%MS[3]%)

输出示例:

208.67.222.222 (42), 208.67.220.220 (44), 4.2.2.2 (47)