如何获得只能在Windows上ping测试的答复线(How to get only the repl

2019-07-31 10:19发布

通常情况下,执行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

我如何在cmd.exe,在回报只有以下行(只有一个ping测试的回复线)通过一个简单的命令在Windows上(无论所使用的Windows语言)?

Reply from <IP address> : bytes=32 time=151 TTL=121

也许最简单的方法就是只显示第二行? 应该如何进行? 因为我不知道如何做到这一点在Windows上。

Answer 1:

这可能工作更普遍。

ping -n 1 <hostname/IP> | FIND "TTL="


Answer 2:

您可以为跳过行选项结合FINDSTR命令:

C:\>ping 127.0.0.1 | for /f "skip=3 tokens=*" %a in ('findstr Reply') do @echo %a

输出是:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

更改%a%%a ,如果写一个批处理文件。



Answer 3:

好,

ping -n 1 <hostname/IP>

将只发送1 ping请求。 你应该能够找到使用莫属了回复线FIND命令。

所以,这样的事情:

ping -n 1 <hostname/IP> | FIND "Reply"

UPDATE

我知道英语,Windows 7计算机上上述作品。 我认为它会为其他本地化工作,但是这可能是一个不正确的假设。

更新2

这个问题似乎提供一些见解。 您可能需要平的输出写入一个文件(使用>输出重定向管道),然后使用该答案的命令只得到第二行。



Answer 4:

这是一个从批处理很简单,但在命令行...丑是主要轻描淡写:

(for /f "skip=1 delims=" %F in ('ping -n 1 localhost') do @if not defined _A @echo %F&set _A=0) &set "_A="

但它确实的伎俩,打印第二行(不管它会发生在含),并跳过休息。 您可以更改行它打印改变skip=

如果你有PowerShell中可用,你可以简单地做:( 是的,我知道这是ping命令是不是应该如何在PS做 ): powershell "ping -n 1 localhost | select -index 2" 。 您可能需要指数玩,因为我(XP)笔记本平中的每一行,它具有双重的效果插入addtional CR - 从PS间距输出。



Answer 5:

您可以使用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,"***")
}

您可以格式化你想要的任何格式的字符串。



Answer 6:

基于@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="

是不错的方法依赖一个班轮不语。
当没有给出响应(超时),其中发现并FINDSTR不会它也会给结果。



文章来源: How to get only the reply line of a ping test on Windows
标签: windows cmd ping