Windows 命令行 - Select 服务器基于 Ping 延迟

Windows Command Line - Select Server Based upon Ping Latency

这里的基本前提是有两个服务器有相同的文件。

我正在使用 Windows 命令行脚本并为其中任一主机打开网络共享。我想编写脚本 "smart" 以便计算出两台服务器之间的延迟并选择延迟最低的主机进行连接。

我想做类似的事情(我知道这段代码不起作用,这是我的概念的一个例子):

PING 192.168.0.1
SET HOST1=%ERRORLEVEL%
PING 192.168.0.2
SET HOST2=%ERRORLEVEL%
IF HOST1 GTR HOST2 GOTO HOST2CONNECT
:HOST1CONNECT
NET USE X: \HOST1 (etc)
:HOST2CONNECT
NET USE X: \HOST2 (etc)

这有意义吗?我似乎无法想象一种比较来自两个 PING 测试的数据的方法,我知道在这种情况下,ERRORLEVEL 只是 1 或 0,具体取决于 ping 是否成功,所以它是垃圾,因为它们会都成功了。但我希望你能理解这个概念,并能看到我遗漏了什么。

提前致谢。

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Initialize variables
    set "selected="
    set "min=99999999"
    set serverList= "bing.com" "duckduckgo.com" "google.es" "google.com"

    echo - Testing -----------------------------

    rem Enumerate the hosts to check
    for %%a in ( %serverList% ) do (

        rem Ping the host and retrieve the average roundtrip
        for /f "tokens=6 delims== " %%r in ('
            ping -n 1 "%%~a" ^| findstr /r /c:"^  .*ms$"
        ') do for /f "delims=ms" %%t in ("%%r") do (
            echo "%%~a" : %%t ms

            rem Determine if the current host has a lower rtt
            rem if %%t geq min or min is already 0, then we have
            rem a division by 0, else a lower rtt has been found
            set /a "1/(min/(%%t+1))" && (
                set "selected=%%~a"
                set "min=%%t"
            )
            rem Of course this can be done with delayed expansion, 
            rem just a question of personal preferences
        )
    ) 2>nul 

    echo(
    echo - Selected ----------------------------
    echo %selected% : %min%

MC ND post在我之前编辑了他的答案,所以他应该得到荣誉。请不要将此标记为答案,这不是我的意图post。

因为我也已经努力 post 回答了,为了完整起见,我觉得 post 另一种方法来处理这个问题并没有什么坏处.主要区别是我选择使用 ping 命令的平均往返时间,并提供一种方法来将延迟时间和目标主机名存储在一组变量中,这些变量可以被视为数组结构。

请记住,使用 ping 命令测量往返延迟不是很准确,结果可能会有很大波动。参考以下来自 Wikipedia 的引述:

Many software platforms provide a service called ping that can be used to measure round-trip latency. Ping performs no packet processing; it merely sends a response back when it receives a packet (i.e. performs a no-op), thus it is a first rough way of measuring latency. Ping cannot perform accurate measurements, principally because it uses the ICMP protocol that is used only for diagnostic or control purposes, and differs from real communication protocols such as TCP. Furthermore, routers and ISP's might apply different traffic shaping policies to different protocols.

@echo off

setlocal enabledelayedexpansion
set targets="192.168.0.1" "192.168.0.2"

for %%e in (%targets%) do (
  call :GetAverageLatency %%e latency

  if errorlevel 1 (
    echo Unable to obtain latency from host: %%~e
    exit /b
  )

  set /a count+=1
  set "host[!count!].name=%%~e"
  set "host[!count!].latency=!latency!"

  if -!latency! gtr -!lowest! (
    set lowest=!latency!
    set index=!count!
  )
)

echo;Mapping network drive to host: !host[%index%].name!
echo;

net use * "\!host[%index%].name!"
set host

exit /b

:GetAverageLatency (__in hostName, __out *latency) {
  for /f "skip=10 tokens=13 delims=m " %%e in ('ping "%~1"') do (
    set "%2=%%e"
    exit /b 0
  )
  exit /b 1
}

set host 命令显然不是必需的,但它用于显示存储在 host* 变量中的值。