2017-02-25 22 views
-2

我試圖製作一個工具,用於ping IP列表並輸出三個IP達到服務器的最低毫秒數(時間)。Windows批處理 - 創建一個工具,可以ping通IP列表並輸出具有最低ms(時間)的三個IP到達服務器

輸入:由spaces-這樣分隔的IP的-list如1.1.1.1 1.1.1.2 1.1.1.3等等

輸出:-the 3級的IP與最低MS-(在最低毫秒量級上從左第三在右最低毫秒),如1.1.2.2 1.2.3.3 1.4.4.4

我有這個迄今平部分:

@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並進行比較。

任何人都可以幫助我嗎?如果我不夠詳細,並且沒有顯示足夠的細節或代碼,我很抱歉。

+0

我明白爲什麼你們downvote,但也有成千上萬的人誰也不把任何努力或代碼到他們的工作,他們獲得了超過100個upvotes。 – djmrminer

回答

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 
+0

有沒有辦法,你不能把它呼應:顯示中間結果 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 – djmrminer

+1

當然,我有意插入這些行以幫助理解該批次的作用,很容易識別2個相關行。 [SO]是程序員搜索和提供幫助的網站。從你最近的問題中,我發現所有的命令都應該很熟悉,但是我錯過了從你身邊學習的努力。所以你不應該感到驚訝,因爲沒有投票。 – LotPings

+0

我找到了一種方法來刪除回聲部分。感謝您提供的信息,我將盡力在創建問題時付出更多努力。 – djmrminer

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)