我正在尝试创建一个ping IP列表并输出具有最低ms(时间)的三个IP来到达服务器的工具。
输入: - 按空格分隔的IP列表 - 例如1.1.1.1 1.1.1.2 1.1.1.3等
输出: - 具有最低ms-的三个IP(按照左边的最低ms到右边的第3个最低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并进行比较。
任何人都能帮助我吗?如果我不够详细,如果我没有显示足够的细节或代码,我很抱歉。
答案 0 :(得分:0)
@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
答案 1 :(得分:0)
@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)