13

Usually, when pinging a server IP address we have this in return:

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)?

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mastermind
  • 161
  • 1
  • 2
  • 8

7 Answers7

23

This may work more universally.

ping -n 1 <hostname/IP> | FIND "TTL="
foxidrive
  • 40,353
  • 10
  • 53
  • 68
5

You can combine findstr command with the skip lines option of 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.

Ofir Luzon
  • 10,635
  • 3
  • 41
  • 52
3

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.

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.

UPDATE 2

This question seems 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.

Community
  • 1
  • 1
mjgpy3
  • 8,597
  • 5
  • 30
  • 51
  • It will not work for all versions of windows(those which have different language than english) – mastermind Sep 07 '12 at 14:22
  • Interesting, terminal commands vary by localization? That strikes me as odd. – mjgpy3 Sep 07 '12 at 14:26
  • @ mjgpy3 : Yes. Language is a problem. Because result of windows ping tool changes when language of operating system changes. Google it and you will see I am right. – mastermind Sep 07 '12 at 14:32
  • maybe displaying only the second line of the answer by a grep? But I don't know how to make it on cmd.exe. – mastermind Sep 07 '12 at 14:34
  • @ mjgspy: I am looking for a direct command actually, like we can do easily on Linux. I do not want to create a file only for a ping. – mastermind Sep 07 '12 at 14:51
  • @mastermind, I understand, I am sorry I was unable to help you. – mjgpy3 Sep 07 '12 at 14:55
2

You can accomplish this with powershell... here is the code you can add to a script

$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.

mhatch73
  • 92
  • 5
2

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=.

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.

wmz
  • 3,645
  • 1
  • 14
  • 22
2

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.

ping -n 1 <hostname/IP> | select-string -pattern 'Reply'
G_Style
  • 588
  • 8
  • 16
1

Based on @wmz answer,

(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.

Mat M
  • 1,786
  • 24
  • 30