按顺序安排多个网站的ping操作?

时间:2018-06-12 22:55:26

标签: batch-file cmd

我已搜索但无法找到可以执行此操作的方法。

这是一个ping我所玩游戏的服务器的批处理,因此我能够为我找到最好的服务器。

有没有办法可以ping所有服务器,然后按照从最慢的响应到最高的顺序列出它们?

我很感激帮助!

@TITLE OSRS Ping Checker
@ECHO off

SET usaworlds=5,6,7,13,14,15,19,20,21,22,23,24,29,30,31,32,37,38,39,40,45,46,47,48,53,54,55,56,57,61,62,69,70,74,77,78,86,117

@ECHO --------------------------------------------------- 
@ECHO                     USA
@ECHO --------------------------------------------------- 
FOR %%i IN (%usaworlds%) DO (
Echo | SET /p=World %%i
FOR /F "tokens=5" %%a IN ('Ping oldschool%%i.runescape.com -n 1 ^| FIND "time="') DO Echo %%a
)

PAUSE

2 个答案:

答案 0 :(得分:0)

@ECHO off
setlocal enabledelayedexpansion
TITLE OSRS Ping Checker

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
del x.tmp 2>nul

SET usaworlds=5,6,7,13,14,15,19,20,21,22,23,24,29,30,31,32,37,38,39,40,45,46,47,48,53,54,55,56,57,61,62,69,70,74,77,78,86,117

ECHO --------------------------------------------------- 
ECHO                     USA
ECHO --------------------------------------------------- 
FOR %%i IN (%usaworlds%) DO (
  <nul set /p "=checking World %%i  !CR!"
  FOR /F "tokens=5" %%a IN ('Ping oldschool%%i.runescape.com -n 1 ^| FIND "TTL="') DO (
    for /f "tokens=2 delims==" %%b in ("%%a") do ( 
      set tim=00000%%b
      set tim=!tim:~-7,-2!
    )
  )
  echo !tim! World %%i>>x.tmp
)
for /f "tokens=3" %%c in ('sort /r x.tmp') do set fastest=%%c
echo fastest response from World %fastest%
PAUSE

注意:一次性ping的时间不可靠(使用ping -t检查不同的时间),因此这种方法可能会给您错误的结果。更好地检查ping -n 5或更高的“平均值”,但这当然会降低脚本的性能。

为了使平均时间更快更快,您可以并行运行ping(我在另一个上下文中使用了该方法years ago

@ECHO off
setlocal 
TITLE OSRS Ping Checker
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
del %temp%\x.tmp 2>nul
SET usaworlds=wrgl,5,6,7,13,14,15,19,20,21,22,23,24,29,30,31,32,37,38,39,40,45,46,47,48,53,54,55,56,57,61,62,69,70,74,77,78,86,117

@ECHO --------------------------------------------------- 
@ECHO                     USA
@ECHO --------------------------------------------------- 
FOR %%i IN (%usaworlds%) DO (
   start /min "Pinging" cmd /v:on /c "(FOR /F "tokens=9 delims= " %%a IN ('Ping oldschool%%i.runescape.com -n 5^|findstr /e "ms"') do set avrg=       %%a)& >> %temp%\x.tmp echo ^!avrg:~-7,-2^!" World %%i
)
:wait
timeout 1 >nul
tasklist /FI "WINDOWTITLE eq Pinging" |find ".exe" >nul && goto :wait
for /f "tokens=3" %%c in ('sort /r %temp%\x.tmp') do set fastest=%%c
echo fastest response from World %fastest%
PAUSE

使用start /min代替start /b可以确保您的屏幕在发生错误时保持清洁(使其慢一点,但您不会注意到它)

答案 1 :(得分:0)

由于有许多主机需要检查/ ping,因此顺序处理会持续很长时间,特别是在使用Stephan recommendation次使用超过回应请求并取平均值时回复时间。

所以我建议使用不同的方法让ping请求在同时进程中发生:

@title OSRS Ping Checker
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ARG=%~1" & if defined _ARG shift /1 & goto :DO_PING & ^
    rem // (jump to label `DO_LOOP` in case arguments are provided)
set "_USAWORLDS=5,6,7,13,14,15,19,20,21,22,23,24,29,30,31,32,37,38,39,40,45,46,47,48,53,54,55,56,57,61,62,69,70,74,77,78,86,117"
set "_ECHOREQUS=10" & rem // (number of echo requests to send per host)
set "_TEMPFILEB=%TEMP%\%~n0_%RANDOM%" & rem // (path and base name of temporary files)
set "_FINALFILE=%~dpn0.txt"           & rem // (path and full name of return file)

rem // Process items in sub-routine but in parallel processes:
for %%I in (%_USAWORLDS%) do (
    rem // Redirect output of every process to individual temporary file:
    > "%_TEMPFILEB%_%%~I.tmp" start /B "" cmd /C "%~f0" :DO_PING %%~I %_ECHOREQUS%
)
rem /* Wait until all temporary files are write-accessible, meaning that
rem    all the parallel processes have been completed/terminated: */
:POLL
for %%I in (%_USAWORLDS%) do (
    rem // Try appending nothing to check for write-access:
    2> nul (>> "%_TEMPFILEB%_%%~I.tmp" rem/) || (
        rem // Wait a bit to not overload the processor:
        > nul timeout /T 1 /NOBREAK
        goto :POLL
    )
)
rem // Combine all individual temporary files into one:
> nul copy /Y "%_TEMPFILEB%_*.tmp" "%_TEMPFILEB%.tmp" /B
rem // Sort data as desired (alphabetic sorting):
sort /R "%_TEMPFILEB%.tmp" /O "%_TEMPFILEB%.tmp"
rem // Create return file, write header:
> "%_FINALFILE%" echo ms      host
rem // Append sorted data to return file:
> nul copy /Y "%_FINALFILE%" + "%_TEMPFILEB%.tmp" "%_FINALFILE%" /B
rem // Clean up temporary files:
del "%_TEMPFILEB%_*.tmp" "%_TEMPFILEB%.tmp"

endlocal
exit /B


:DO_PING
    rem // Build host URL to ping, set number of echo requests to send:
    set "URL=oldschool%~1.runescape.com"
    set /A "NUM=%~2"
    rem /* Perform ping and capture last line of response, which should contain
    rem    the average reply time: */
    set "STR="
    for /F "delims=" %%P in ('2^> nul ping "%URL%" -n %NUM%') do set "STR=%%P"
    rem // Check whether last line of response contains average reply time:
    if not defined STR exit /B
    set "AVG=%STR:*Average =%"
    set "AVG=%AVG:~1%"
    if "%AVG%"=="%STR%" exit /B
    rem /* Convert average reply time to pure left-zero-padded number; the padding
    rem    is intended to simplify the later (purely alphabetic) sorting: */
    set /A "AVG=AVG"
    set "AVG=000000%AVG%"
    rem // Return average reply time together with respective host URL:
    echo %AVG:~-6%  "%URL%"
    exit /B