问题描述
这是对我玩的游戏的服务器进行 ping 操作的批处理,以便我能够找到最适合我的服务器.
This is a batch that pings the servers of a game I play, so that I am able to find the best server for me.
有没有办法让它ping所有服务器,然后按响应从最慢到最高的顺序列出它们?
Is there a way I can have it ping all the servers then list them in order from the slowest response to the highest?
@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
推荐答案
由于要检查/ping 的主机很多,因此顺序处理会持续很长时间,尤其是在关注Stephan 的建议使用多个回显请求并占用平均回复时间.
Since there are many hosts to check/ping, sequencial processing lasts for a quite long time, particularly when following Stephan's recommendation of using more than one echo requests and taking the average reply time.
所以我建议使用不同的方法,让 ping 请求发生在同时的进程中:
So I suggest to use a different approach and let the ping requests happen in simultaneous processes:
@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
这篇关于批量排列多个网站的ping顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!