Ping with timestamp on Windows CLI

351,049

Solution 1

@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

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Above command gives you output in a file like 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
Share:
351,049
SuicideSheep
Author by

SuicideSheep

"If you see scary things, look for the helpers- you'll always see people helping" -Fred Rogers

Updated on July 14, 2021

Comments

  • SuicideSheep
    SuicideSheep almost 3 years

    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

  • SuicideSheep
    SuicideSheep almost 10 years
    I've tested with your way however i got %%a was unexpected at this time?
  • SuicideSheep
    SuicideSheep almost 10 years
    Yes of course..Could it be there's some typo in between?
  • MC ND
    MC ND almost 10 years
    @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.
  • Ryan
    Ryan over 9 years
    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?
  • MC ND
    MC ND over 9 years
    @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
    MC ND over 8 years
    @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!
  • allwynmasc
    allwynmasc over 8 years
    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?
  • sabo-fx
    sabo-fx over 8 years
    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"
  • Asprelis
    Asprelis over 8 years
    @sabo-fx sometimes 'sleep 1 >NUL' not works(not recognized command), i used 'timeout 1 >NUL'
  • sabo-fx
    sabo-fx over 8 years
    @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.
  • Dan
    Dan over 8 years
    no response on ping, so I did control+C and computer got slow, froze then blue screen
  • MC ND
    MC ND over 8 years
    @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?
  • Dan
    Dan over 8 years
    @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.
  • Hicsy
    Hicsy almost 8 years
    404 on that link now
  • mikia
    mikia over 7 years
    I tested the script. Work fine, but you must add one space char in 3. line after set and before /p.
  • sabo-fx
    sabo-fx over 7 years
    @mika Thanx for your suggestion. I've corrected the code.
  • Serinus
    Serinus over 7 years
    Worked beautifully without any of the infinite loop/max cpu issues of the other suggestions.
  • Admin
    Admin about 7 years
    Perdona, coloqué "un alias de servidor" coloca la direcciónIP en lugar de la cadena "sntdn", saludos
  • Jorge Cornejo Bellido
    Jorge Cornejo Bellido about 7 years
    I have tried to add the -l 1024 parameter to the ping result but no luck... How to add another parameters? Thanks!
  • MC ND
    MC ND about 7 years
    @JorgeCornejoBellido, I've added it to the source ping before the -t without problems.
  • Rhak Kahr
    Rhak Kahr over 6 years
    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.
  • Mike
    Mike over 6 years
    it doesn't work anymore, it says "time" instead of the actual time
  • MC ND
    MC ND over 6 years
    @Mike, Tested now. After copy, paste into a batch file and execute (W10.0.15063 64b), it works.
  • Satish
    Satish over 6 years
    This is best so far
  • Phasma Felis
    Phasma Felis almost 6 years
    @sabo-fx Thanks for this! What will it do if a ping takes multiple seconds to resolve, or times out completely?
  • Serinus
    Serinus almost 6 years
    Test-Connection -Count 9999 -ComputerName google.com | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAd‌​dress,ResponseTime | tee -file C:\Users\yourname\Desktop\pingtest.txt -append
  • Shoaib Khan
    Shoaib Khan almost 6 years
    Works perfectlly!!
  • Otto medina
    Otto medina almost 6 years
    Windows 7: This completely killed my machine. Kept starting new cmd.exe processe. Don't try this at home!
  • Otto medina
    Otto medina almost 6 years
    This does NOT work on Windows, as was asked in the question.
  • MC ND
    MC ND almost 6 years
    @Janos, How did you name your batch file?
  • sabo-fx
    sabo-fx almost 6 years
    @Janos: Can you provide me with more details, of what is/isn't working? Do you receive an error message?
  • Otto medina
    Otto medina over 5 years
    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.
  • Joe Irby
    Joe Irby over 5 years
    As of September 8 2018, the posted code works perfectly for me on Windows 10 without changes. Thanks!
  • Sree Harsha
    Sree Harsha over 5 years
    simple and crisp solution. first option worked for me
  • Lee
    Lee over 5 years
    This works just fine on Windows, under Windows Subsystem for Linux (WSL) which is an optional feature.
  • skomisa
    skomisa about 5 years
    This solution is identical to one provided in an earlier answer to this question.
  • Himanshu Shekhar
    Himanshu Shekhar about 5 years
    I had been trying to log ping results. date +%s is more helpful as it shows integer timestamps.
  • jorisw
    jorisw almost 5 years
    On Mac, -w is an invalid option to ping.
  • prem
    prem over 4 years
    It's November 2019 and still it works perfectly without any modifications on Windows 10.
  • khalidmehmoodawan
    khalidmehmoodawan over 4 years
    @Janos : This works in Windows. Please use Windows PowerShell.
  • khalidmehmoodawan
    khalidmehmoodawan over 4 years
    @skomisa : it is a little different as it is also saving to file that can be later used.
  • mwfearnley
    mwfearnley over 4 years
    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,ProtocolAd‌​dress,ResponseTime
  • flandersen
    flandersen almost 4 years
    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
    flandersen almost 4 years
    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.
  • Keith E. Truesdell
    Keith E. Truesdell almost 4 years
    this is exactly what I am looking for! thank you...works perfectly. i get the time and then the output i would expect from a ping line by line
  • Dilhan Nakandala
    Dilhan Nakandala over 3 years
    If I use this batch and save the output to a txt file, does it record request time outs?
  • MC ND
    MC ND over 3 years
    @DilhanNakandala, The code is not filtering the output of the ping command so request timeouts should be echoed.
  • Marcus Nyberg
    Marcus Nyberg over 3 years
    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.
  • MC ND
    MC ND over 3 years
    @MarcusNyberg, How did you name the batch file?
  • Marcus Nyberg
    Marcus Nyberg over 3 years
    @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"
  • MC ND
    MC ND over 3 years
    @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).
  • NessDan
    NessDan about 2 years
    -D Print timestamp (unix time + microseconds as in gettimeofday) before each line. You can update this, ping now comes built-in with this functionality.
  • afaf12
    afaf12 almost 2 years
    @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.