【问题标题】:Batch: want to sort the web addresses by ping time and write it to file批处理:想按ping时间对网址进行排序并写入文件
【发布时间】:2021-06-12 21:03:32
【问题描述】:

我想对有关其 ping 时间的网站列表进行排序。 我编写了以下代码来找到最快的 ping 时间 但是当我将循环放入另一个循环以对列表进行排序时,我得到 ECHO 在输出文件中为 OFF。 谁能帮我完成代码并帮助我处理 ECHO 输出?

@echo off
setlocal enableextensions disabledelayedexpansion

echo #Autogenerated regarding ping time> sitePingList.txt

set serverList= "www.site1.com" "www.site2.com" "www.site3.com"

rem Initialize variables
set "selected="
set "min=99999999"

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"
        )
    )
) 2>nul 

echo - Selected ----------------------------
echo %selected% : %min%
set ping1="fastest1: %selected%, user:test, password:123456"
set ping1=%ping1:"=%
>>  sitePingList.txt echo %ping1%

我检查了@lit 所说的内容: $result 值如下:

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
NIMA-LAPTOP   www.ibm.com     148.251.160.242                                           32       100
NIMA-LAPTOP   www.hp.com      23.58.222.80                                              32       151

如您所见,输出是两行简单的行,只有 ms 没有其他文本。 另请注意,您应该更改 testConnection 的语法并将 targetName 更改为 computerName

PS C:\Users\Nima> $Targets = @('www.ibm.com', 'www.hp.com')
>> $Results = @()
>> foreach ($Target in $Targets) {
>>     $Results += Test-Connection -Count 1 -ComputerName $Target
>> }
>> $Results |
>>     Sort-Object -Property Latency |
>>     ForEach-Object {
>>         "{0} {1}ms" -f @($_.Destination, $_.Latency)
>>     }
 ms
 ms

【问题讨论】:

  • 如果这是您的完整脚本,那么%ping1% 不可能为空,这将是echo is OFF 出现在sitePingList.txt 中的唯一可能条件。你确定你写的文件和你检查的文件是一样的吗? (检查日期/时间戳)。

标签: batch-file cmd ping


【解决方案1】:

此脚本 ping 目标列表并按延迟对结果进行排序。可以使用以下方式调用它:

powershell -NoLogo -NoProfile -File ".\Get-PingList.ps1"

=== Get-PingList.ps1

$Targets = @('www.ibm.com', 'www.hp.com')
$Results = @()
foreach ($Target in $Targets) {
    $Results += Test-Connection -Count 1 -TargetName $Target
}
$Results |
    Sort-Object -Property Latency |
    ForEach-Object {
        "{0} {1}ms" -f @($_.Destination, $_.Latency)
    }

=== 示例输出

PS C:\src\t> .\Get-FastestPing.ps1
www.hp.com 46ms
www.ibm.com 54ms

=== 所有信息均来自Test-Connection

PS C:\src\t> Test-Connection -Count 1 -TargetName 'ibm.com' | Format-List * -Force

Ping           : 1
Source         : NB-XXXXXXX
Destination    : ibm.com
Address        : 184.87.3.183
DisplayAddress : 184.87.3.183
Latency        : 59
Status         : Success
BufferSize     : 32
Reply          : System.Net.NetworkInformation.PingReply

【讨论】:

  • 我应该把你的代码改成这样,这样它就可以在我的 PS 上运行,但它有空输出:'''$Targets = @('www.ibm.com', 'www.hp.com' ) $Results = @() foreach ($Target in $Targets) { $Results += Test-Connection -Count 1 -ComputerName $Target } $Results = $Results |排序对象-属性时间 $Results | ForEach-Object {"{0} {1}ms" -f $_.Destination, $_.Time }'''
  • 有输出吗?错误信息?您可以手动 ping www.ibm.com 或 www.hp.com 成功吗?你为什么把代码放在评论里? Test-Connection 似乎在 PowerShell 3 中可用,所以我认为这不是版本问题。
  • 我没有把代码放在注释中,我以为它会在这里显示为格式化文本,但我错了,懒得在这里更正。如前所述,代码需要 computerName 而不是 targetName。它会正确显示结果并正确显示排序列表,但是当使用最后一行时:foreac-object 它只输出两个带有 ms 的换行符。这表明格式化算法无法正常工作。我的 PS 版本使用这个命令 Get-Host |选择对象是 5.1.19041.1023
  • @NimaNouri,我已添加到答案信息中,希望对您有所帮助。 pingTest-Connection 都不会产生 COMPUTERNAME。这是主机名的 Microsoft 术语。当您通过 URL ping 网站时,主机名是未知的。
  • PS C:\Users\Nima> 测试连接 -Count 1 -TargetName 'ibm.com' | Format-List * -Force Test-Connection:找不到与参数名称“TargetName”匹配的参数。在 line:1 char:26 + Test-Connection -Count 1 -TargetName 'ibm.com' | Format-List * -Force + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Test-Connection], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.TestConnectionCommand 我唯一的答案告诉你是你对命令的语法错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多