如何在 Windows 上仅获取 ping 测试的回复行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12319836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get only the reply line of a ping test on Windows
提问by mastermind
Usually, when pinging a server IP address we have this in return:
通常,当 ping 一个服务器 IP 地址时,我们会得到这样的回报:
Pinging <IP address> with 32 bytes of data:
Reply from <ip> : bytes=32 time=151 TTL=121
Reply from <ip> : bytes=32 time=151 TTL=121
Reply from <ip> : bytes=32 time=151 TTL=121
Reply from <ip> : bytes=32 time=151 TTL=121
Ping statistics for <IP address>:
packets: sent = 4, Received = 4, lost = 0 (0% loss),
Approximate round trip times in milli-secounds:
Minimum = 151ms, Maximum = 151 ms, Average = 151 ms
How do I get only the following line (only the reply line of one ping test) in return by a simple command in cmd.exe on Windows (whatever Windows language used)?
如何通过 Windows 上的 cmd.exe 中的简单命令仅获得以下行(仅一个 ping 测试的回复行)作为回报(无论使用什么 Windows 语言)?
Reply from <IP address> : bytes=32 time=151 TTL=121
Maybe the simplest way is to display only the second line? How should this be done? Because I don't know how to do it on Windows.
也许最简单的方法是只显示第二行?这应该怎么做?因为我不知道如何在 Windows 上做到这一点。
回答by foxidrive
This may work more universally.
这可能更普遍。
ping -n 1 <hostname/IP> | FIND "TTL="
回答by Ofir Luzon
You can combine findstr command with the skip lines option of for:
您可以将 findstr 命令与 for 的跳过行选项结合使用:
C:\>ping 127.0.0.1 | for /f "skip=3 tokens=*" %a in ('findstr Reply') do @echo %a
Output is:
输出是:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Change %a
to %%a
if writing a batch file.
如果写入批处理文件,则更%a
改为%%a
。
回答by mjgpy3
Well,
好,
ping -n 1 <hostname/IP>
Will send only 1 ping request. And you should be able to find the reply line using none other than the FIND
command.
将仅发送 1 个 ping 请求。并且您应该能够使用该FIND
命令找到回复行。
So, something like this:
所以,像这样:
ping -n 1 <hostname/IP> | FIND "Reply"
UPDATE
更新
I know the above works on an English, Windows 7 machine. I would assume that it would work for other localizations, but this may be an incorrect assumption.
我知道以上工作在英语的 Windows 7 机器上。我认为它适用于其他本地化,但这可能是一个不正确的假设。
UPDATE 2
更新 2
This questionseems to provide some insight. You may have to write the output of ping to a file (using the >
output redirection pipe) and then use the commands in the answers to get only the second line.
这个问题似乎提供了一些见解。您可能必须将 ping 的输出写入文件(使用>
输出重定向管道),然后使用答案中的命令仅获取第二行。
回答by wmz
It's pretty simple from within batch, but from command line... ugly is major understatement:
在批处理中非常简单,但是从命令行......丑陋是主要的轻描淡写:
(for /f "skip=1 delims=" %F in ('ping -n 1 localhost') do @if not defined _A @echo %F&set _A=0) &set "_A="
But it does the trick, prints second line (whatever it will happen to contain) and skips the rest. You may change line it prints changing skip=
.
但它可以解决问题,打印第二行(无论它将包含什么内容)并跳过其余部分。您可以更改它打印的行更改skip=
。
If you have powershell available, you could simply do: (yes I know it's not how pinging is supposed to be done in PS): powershell "ping -n 1 localhost | select -index 2"
. You may need to play with index, as on my (XP) laptop ping inserts addtional CR in each line, which has an effect of double - spacing output from PS.
如果你有PowerShell中可用,你可以简单地做:(是的,我知道这是ping命令是不是应该如何在PS做)powershell "ping -n 1 localhost | select -index 2"
。您可能需要使用索引,因为在我的 (XP) 笔记本电脑上,ping 在每行中插入了额外的 CR,这会产生 PS 的双倍间距输出效果。
回答by mhatch73
You can accomplish this with powershell... here is the code you can add to a script
您可以使用 powershell 完成此操作...这是您可以添加到脚本中的代码
$ping = new-object System.Net.NetworkInformation.Ping
$reply = $ping.send('127.0.0.1')
if ($reply.status -eq "Success"){
[string]::Format("Reply from {0},time={1}",$reply.Address.ToString(),$reply.RoundtripTime)
}else{
$z = [system.net.dns]::gethostaddresses($hostname)[0].ipaddresstostring
[string]::Format("FAIL,{0},{1}",$z,"***")
}
You can format the string in any format you want.
您可以将字符串格式化为您想要的任何格式。
回答by Mat M
Based on @wmz answer,
基于@wmz 的回答,
(for /f "skip=3 tokens=*" %F in ('ping -n 1 <hostname/IP>') do @if not defined _A @echo %F&set _A=0) &set "_A="
is ok as a one-liner not language dependent.
It will also give the result when no response is given (Timeout), where find and findstr will not.
可以作为单线不依赖于语言。
当没有给出响应(超时)时,它也会给出结果,其中 find 和 findstr 不会。
回答by G_Style
For some reason, I couldn't get the FIND pipe to work in a powershell 5.1 shell. Feeling like this was all overly complicated, I thought if only I could grep the Reply line? I then found the powershell equivalent! I just used select-string on the first line matching 'Reply' which gave me the one line output I was looking for. So this worked very simply and easy for me.
出于某种原因,我无法让 FIND 管道在 powershell 5.1 shell 中工作。感觉这一切都过于复杂,我想如果我能grep回复行吗?然后我找到了等效的powershell!我只是在匹配“回复”的第一行上使用了选择字符串,这给了我我正在寻找的一行输出。所以这对我来说非常简单和容易。
ping -n 1 <hostname/IP> | select-string -pattern 'Reply'