114

On the Windows command prompt cmd, I use ping -t to 10.21.11.81

Reply from 10.21.11.81: bytes=32 time=3889ms TTL=238
Reply from 10.21.11.81: bytes=32 time=3738ms TTL=238
Reply from 10.21.11.81: bytes=32 time=3379ms TTL=238

Are there any possibilities to get an output like this?

10:13:29.421875 Reply from 10.21.11.81: bytes=32 time=3889ms TTL=238
10:13:29.468750 Reply from 10.21.11.81: bytes=32 time=3738ms TTL=238
10:13:29.468751 Reply from 10.21.11.81: bytes=32 time=3379ms TTL=238

Please note that I wanna achieve this with only commands provided by CMD

John Mee
  • 50,179
  • 34
  • 152
  • 186
SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
  • possible duplicate of [How to timestamp every ping result?](http://stackoverflow.com/questions/10679807/how-to-timestamp-every-ping-result) – kaz Jun 03 '15 at 15:17
  • 1
    A more generic version: https://stackoverflow.com/questions/21564/is-there-a-unix-utility-to-prepend-timestamps-to-stdin – Anton Tarasenko Nov 03 '19 at 18:26

18 Answers18

141

WindowsPowershell:

option 1

ping.exe -t COMPUTERNAME|Foreach{"{0} - {1}" -f (Get-Date),$_}

option 2

Test-Connection -Count 9999 -ComputerName COMPUTERNAME | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAddress,ResponseTime
Morvader
  • 2,317
  • 3
  • 31
  • 44
Hames
  • 1,569
  • 1
  • 9
  • 6
  • 4
    Worked beautifully without any of the infinite loop/max cpu issues of the other suggestions. – Serinus Nov 29 '16 at 22:19
  • 1
    Test-Connection -Count 9999 -ComputerName google.com | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAddress,ResponseTime | tee -file C:\Users\yourname\Desktop\pingtest.txt -append – Serinus Jun 01 '18 at 15:28
  • simple and crisp solution. first option worked for me – Sree Harsha Oct 22 '18 at 12:40
  • If you want to paste Option 2 straight into a PowerShell prompt, you can omit the -ComputerName bit, and it will prompt you for list of ComputerNames (just leave the second one blank). `Test-Connection -Count 9999 | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAddress,ResponseTime` – mwfearnley Jan 28 '20 at 12:47
  • 1
    Option 1 is a winner over Option 2 if ping requests time out because Option 2 outputs a PowerShell exception instead of "Request timed out.". – flandersen May 14 '20 at 07:34
  • 2
    Extending the Options above with `> C:\temp\ping.txt` redirects the output to a file. If you want to see the live output as well, simply execute `Get-Content C:\temp\ping.txt -tail 10 -wait` in another PowerShell. – flandersen May 14 '20 at 07:39
109
@echo off
    ping -t localhost|find /v ""|cmd /q /v:on /c "for /l %%a in (0) do (set "data="&set /p "data="&if defined data echo(!time! !data!)" 

note: code to be used inside a batch file. To use from command line replace %%a with %a

Start the ping, force a correct line buffered output (find /v), and start a cmd process with delayed expansion enabled that will do an infinite loop reading the piped data that will be echoed to console prefixed with the current time.

2015-01-08 edited: In faster/newer machines/os versions there is a synchronization problem in previous code, making the set /p read a line while the ping command is still writting it and the result are line cuts.

@echo off
    ping -t localhost|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost>nul"

Two aditional pause commands are included at the start of the subshell (only one can be used, but as pause consumes a input character, a CRLF pair is broken and a line with a LF is readed) to wait for input data, and a ping -n 2 localhost is included to wait a second for each read in the inner loop. The result is a more stable behaviour and less CPU usage.

NOTE: The inner ping can be replaced with a pause, but then the first character of each readed line is consumed by the pause and not retrieved by the set /p

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I've tested with your way however i got `%%a was unexpected at this time`? – SuicideSheep Jul 23 '14 at 09:53
  • Yes of course..Could it be there's some typo in between? – SuicideSheep Jul 23 '14 at 09:55
  • 10
    @Mr.SuicideSheep, this is a batch file. If you want to test it from command line, replace all the double percent signs (that need to be escaped inside batch files) with single percent signs. – MC ND Jul 23 '14 at 09:56
  • 5
    When I try to use this, I end up getting line breaks in the output. They routinely appear after the 52nd character in the output. Any thoughts as to why? – Ryan Jan 08 '15 at 12:24
  • @Ryan, answer updated. I was using a old XP machine when I included the original code. Tested now in a i3 windows 7 x64 and the cut problem raises. Test new code. – MC ND Jan 08 '15 at 13:31
  • 2
    @AlexG, if you need the date (the original question didn't include it in the desired output), change the `echo` command to `echo(!date! !time! !data!` – MC ND Sep 01 '15 at 05:42
  • 4
    no response on ping, so I did control+C and computer got slow, froze then blue screen – Dan Nov 08 '15 at 04:28
  • 2
    @Dan, The only way I can think to get a BSOD running this code (discarding hw or driver problems) is a fork bomb. Have you called your batch file `ping.bat` or `ping.cmd`? – MC ND Nov 08 '15 at 08:44
  • @MCND .bat, I tried again on a clean virtual computer, it worked fine. I tried again on my regular system, this time with taskkill ready to run, I got hundreds of cmd.exe instances. I'm sure my system is clean, so it should be a configuration issue. Will look into that, thanks. – Dan Nov 08 '15 at 19:18
  • I have tried to add the -l 1024 parameter to the ping result but no luck... How to add another parameters? Thanks! – Jorge Cornejo Bellido Mar 27 '17 at 16:45
  • @JorgeCornejoBellido, I've added it to the source ping before the `-t` without problems. – MC ND Mar 27 '17 at 17:45
  • ping localhost -n 5 | find /v "" | cmd /q /v:on /c "for /l %a in (0) do (set "data="&set /p "data="&if defined data echo(!date! !time! !data!)" Another 'challenge' is raised. How to eliminate CTRL+C requirement after the 5th ping. Any improvement are welcome. – Rhak Kahr Aug 25 '17 at 02:08
  • it doesn't work anymore, it says "time" instead of the actual time – Mike Sep 28 '17 at 08:23
  • @Mike, Tested now. After copy, paste into a batch file and execute (W10.0.15063 64b), it works. – MC ND Sep 28 '17 at 08:32
  • Windows 7: This completely killed my machine. Kept starting new cmd.exe processe. Don't try this at home! – Janos Jul 19 '18 at 11:22
  • 1
    @Janos, How did you name your batch file? – MC ND Jul 19 '18 at 14:22
  • Doh! I named it ping.bat. That's a terrible idea, as it causes recursion without running the actual ping command. Thanks for the hint. It seems like the script does work indeed. – Janos Jul 31 '18 at 13:18
  • If I use this batch and save the output to a txt file, does it record `request time outs`? – Dilhan Nakandala Aug 18 '20 at 06:12
  • @DilhanNakandala, The code is not filtering the output of the ping command so request timeouts should be echoed. – MC ND Aug 18 '20 at 08:35
  • Tried this in Windows 10 with a bat-file. My computer froze like never before. Even taskmanager couldn't kill the process. Had to forcefully restart my computer and lost some other work. – Marcus Nyberg Oct 27 '20 at 07:30
  • @MarcusNyberg, How did you name the batch file? – MC ND Oct 27 '20 at 15:26
  • @MC ND I named it ping.bat . If the naming is causing the freezing-problem you should add that as a Note. "Do not name the bat file ping.bat it will crash your computer" – Marcus Nyberg Oct 29 '20 at 12:29
  • @MarcusNyberg, It is not just ping.bat. The problem is that the script code calls ping (and cmd). As your file is also called ping, it will call itself again, and again,... Can it be avoided? Yes, we can use full paths for each invoked command. Why did I not include them? Because it is just sample code explaining the concept, not a bulletproof solution (it is very, very far from it). – MC ND Oct 29 '20 at 15:02
  • @MCND I know this is a long shot, but how would you extend the example if you wanted to call a different batch file when a timeout happened? The second batch file would be one with network diagnostic functions, and it would save to a different log file. I've been trying to implement this from scratch, and also to adapt your example. So far, unsuccessfully. – afaf12 May 29 '22 at 19:02
75

You can do this in Bash (e.g. Linux or WSL):

ping 10.0.0.1 | while read line; do echo `date` - $line; done

Although it doesn't give the statistics you usually get when you hit ^C at the end.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Sam Critchley
  • 3,388
  • 1
  • 25
  • 28
32

Batch script:

@echo off

set /p host=host Address: 
set logfile=Log_%host%.log

echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL 
    GOTO Ping)

This script will ask for which host to ping. Ping output is output to screen and log file. Example log file output:

Target Host = www.nu.nl
Pinging nu-nl.gslb.sanomaservices.nl [62.69.166.210] with 32 bytes of data: 
24-Aug-2015 13:17:42 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250
24-Aug-2015 13:17:43 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250
24-Aug-2015 13:17:44 Reply from 62.69.166.210: bytes=32 time=1ms TTL=250

Log file is named LOG_[hostname].log and written to same folder as the script.

iono
  • 2,575
  • 1
  • 28
  • 36
sabo-fx
  • 462
  • 4
  • 6
  • keeps saying sleep is not recognized but the script works fine. How do I stop the result from keeping echoing in the cmd window and just keep only going to the log file? – allwynmasc Sep 16 '15 at 08:04
  • You should download and extract sleep.zip to the same folder as from where you execute the batch file. The sleep command forces the script to wait 1 second after each ping. Without it, there is no pause between 'pings' and your logfile will grow very fast. To prevent showing results in the cmd window, you can remove **the last line** from the script that starts with the word "echo" – sabo-fx Sep 19 '15 at 10:19
  • @sabo-fx sometimes 'sleep 1 >NUL' not works(not recognized command), i used 'timeout 1 >NUL' – Asprelis Oct 22 '15 at 09:01
  • @Asprelis: That's awesome. Thanx for the tip. I was unaware of the existence of the "timeout" command. I agree that its much nicer to use commands that are included with the OS. – sabo-fx Oct 24 '15 at 01:37
  • I tested the script. Work fine, but you must add one space char in 3. line after set and before /p. – mikia Aug 28 '16 at 08:57
  • @mika Thanx for your suggestion. I've corrected the code. – sabo-fx Aug 28 '16 at 18:32
  • @sabo-fx Thanks for this! What will it do if a ping takes multiple seconds to resolve, or times out completely? – Phasma Felis May 30 '18 at 03:06
  • @Janos: Can you provide me with more details, of what is/isn't working? Do you receive an error message? – sabo-fx Jul 20 '18 at 11:42
  • As of September 8 2018, the posted code works perfectly for me on Windows 10 without changes. Thanks! – Joe Irby Sep 08 '18 at 16:48
  • It's November 2019 and still it works perfectly without any modifications on Windows 10. – prem Nov 04 '19 at 07:15
  • 1
    It's July 2022 and still it is working perfectly without any modifications on Windows 10. – Jayakumari Arumugham Jul 17 '22 at 17:57
24

This might help someone : [Needs to be run in Windows PowerShell]

ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt

-- Check for the Ping_IP.txt file at the current directory or user home path.

The above command gives you output in a file like the below ;

9/14/2018 8:58:48 AM - Pinging 10.227.23.241 with 32 bytes of data:
9/14/2018 8:58:48 AM - Reply from 10.227.23.241: bytes=32 time=29ms TTL=117
9/14/2018 8:58:49 AM - Reply from 10.227.23.241: bytes=32 time=29ms TTL=117
9/14/2018 8:58:50 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117
9/14/2018 8:58:51 AM - Reply from 10.227.23.241: bytes=32 time=27ms TTL=117
9/14/2018 8:58:52 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117
9/14/2018 8:58:53 AM - Reply from 10.227.23.241: bytes=32 time=27ms TTL=117
9/14/2018 8:58:54 AM - Reply from 10.227.23.241: bytes=32 time=28ms TTL=117

Good Luck !!!

khalidmehmoodawan
  • 598
  • 1
  • 5
  • 22
7

This might fit the bill for later Windows versions:

for /l %i in (1,0,2) do @echo|cmd /v:on /c set /p=!time! & ping -n 1 10.21.11.81 | findstr "Reply timed" && timeout /t 2 > nul:
double-beep
  • 5,031
  • 17
  • 33
  • 41
mlarss
  • 71
  • 1
  • 2
6

On Windows

You can use one of the other answers.

On Unix/Linux

while :;do ping -n -w1 -W1 -c1 10.21.11.81| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done

Or as function pingt for your ~/.bashrc:

pingt() {
  while :;do ping -n -w1 -W1 -c1 $1| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done
}

source: https://stackoverflow.com/a/26666549/1069083

Community
  • 1
  • 1
rubo77
  • 19,527
  • 31
  • 134
  • 226
4

I think my code its what everyone need:

ping -w 5000 -t -l 4000 -4 localhost|cmd /q /v /c "(pause&pause)>nul &for /l %a in () do (for /f "delims=*" %a in ('powershell get-date -format "{ddd dd-MMM-yyyy HH:mm:ss}"') do (set datax=%a) && set /p "data=" && echo([!datax!] - !data!)&ping -n 2 localhost>nul"

to display:

[Fri 09-Feb-2018 11:55:03] - Pinging localhost [127.0.0.1] with 4000 bytes of data:
[Fri 09-Feb-2018 11:55:05] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:08] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:11] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128
[Fri 09-Feb-2018 11:55:13] - Reply from 127.0.0.1: bytes=4000 time<1ms TTL=128

note: code to be used inside a command line, and you must have powershell preinstalled on os.

Eldar Value
  • 161
  • 1
  • 8
2

Try this:

Create a batch file with the following:

echo off

cd\

:start

echo %time% >> c:\somedirectory\pinghostname.txt

ping pinghostname >> c:\somedirectory\pinghostname.txt

goto start

You can add your own options to the ping command based on your requirements. This doesn't put the time stamp on the same line as the ping, but it still gets you the info you need.

An even better way is to use fping, go here http://www.kwakkelflap.com/fping.html to download it.

rubo77
  • 19,527
  • 31
  • 134
  • 226
jjrab
  • 21
  • 3
2

Use

ping -D 8.8.8.8

From the man page

-D     Print timestamp (unix time + microseconds as in gettimeofday) before each line

Output

[1593014142.306704] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=120 time=13.7 ms
[1593014143.307690] 64 bytes from 8.8.8.8: icmp_seq=3 ttl=120 time=13.8 ms
[1593014144.310229] 64 bytes from 8.8.8.8: icmp_seq=4 ttl=120 time=14.3 ms
[1593014145.311144] 64 bytes from 8.8.8.8: icmp_seq=5 ttl=120 time=14.2 ms
[1593014146.312641] 64 bytes from 8.8.8.8: icmp_seq=6 ttl=120 time=14.8 ms
Abbas Gadhia
  • 14,532
  • 10
  • 61
  • 73
1

Try this instead:

ping -c2 -s16 sntdn | awk '{print NR " | " strftime("%Y-%m-%d_%H:%M:%S") " | " $0  }'

Check if it suits you

TheTanadu
  • 554
  • 1
  • 7
  • 33
JuanZR
  • 19
  • 1
  • 1
    Perdona, coloqué "un alias de servidor" coloca la direcciónIP en lugar de la cadena "sntdn", saludos – JuanZR Mar 14 '17 at 17:35
1

I also need this to monitor the network issue for my database mirroring time out issue. I use the command code as below:

ping -t Google.com|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!date! !time! !data!)&ping -n 2 Google.com>nul" >C:\pingtest.txt

You just need to modify Google.com to your server name. It works perfectly for me. and remember to stop this when you finished. The pingtest.txt file will increase by 4.5 KB per min (around).

Thank for raymond.cc. https://www.raymond.cc/blog/timestamp-ping-with-hrping/

DBALUKE HUANG
  • 247
  • 1
  • 10
1

ping -t wwww.google.com|cmd /q /v /c “(pause&pause)>nul & for /l %a in () do (set /p “data=” && echo(!date! !time! !data!)&ping -n 2 wwww.google.com>nul”

Mahdi Loghmani
  • 361
  • 3
  • 2
0

Another powershell method (I only wanted failures)

$ping = new-object System.Net.NetworkInformation.Ping
$target="192.168.0.1"
Write-Host "$(Get-Date -format 's') Start ping to $target"
while($true){
    $reply = $ping.send($target)
    if ($reply.status -eq "Success"){
        # ignore success    
        Start-Sleep -Seconds 1
    }
    else{
        Write-Host "$(Get-Date -format 's') Destination unreachable" $target

    }
}
0

An enhancement to MC ND's answer for Windows.
I needed a script to run in WinPE, so I did the following:

@echo off
SET TARGET=192.168.1.1
IF "%~1" NEQ "" SET TARGET=%~1

ping -t %TARGET%|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost >nul"

This can be hardcoded to a particular IP Address (192.168.1.1 in my example) or take a passed parameter. And as in MC ND's answer, repeats the ping about every 1 second.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
0

Simple :

@echo off

set hostName=www.stackoverflow.com
set logfile=C:\Users\Dell\Desktop\PING_LOG\NetworkLog\Log_%hostName%.text
echo Network Loging Running %hostName%...
echo Ping Log %hostName% >>%logfile%

:Ping
for /f "tokens=* skip=2" %%A in ('ping %hostName% -n 1 ') do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    timeout 1 >NUL
    GOTO Ping)
Thrainder
  • 77
  • 1
  • 10
0

Instead of having the additional ping -n 2 localhost at the end of the loop, you can just add the character R before !data! since the only possibilities are Reply or Request. The first character is consumed from the pause>nul. So instead of having the following expression:

ping localhost -t -l 4|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p data=&echo(!date! !time! !data!)&ping -n 2 localhost>nul"

You can use this expression:

ping localhost -t -l 4|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p data=&echo(!date! !time! R!data!)&pause>nul"

Which produces the same output eg.:

22:34:49.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:50.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:55.47 Request timed out.
22:34:56.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
22:34:57.49 Reply from 172.217.4.46: bytes=4 time=14ms TTL=116
Vorpal56
  • 51
  • 2
0

Here's one liner for Windows CMD , that writes in a file with timestamp (with infinite loop on ping)

FOR /L %N IN () DO date /t >>ping.txt && time /t >>ping.txt && ping google.com -n 4 >>ping.txt

Here's a version that pings only 4 times with timestamp :

date /t >>ping.txt && time /t >>ping.txt && ping google.com -n 4 >>ping.txt
A W
  • 1,041
  • 11
  • 18