Ping with timestamp on Windows CLI
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
SuicideSheep
"If you see scary things, look for the helpers- you'll always see people helping" -Fred Rogers
Updated on July 14, 2021Comments
-
SuicideSheep almost 3 years
On the Windows command prompt
cmd
, I useping -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 almost 10 yearsI've tested with your way however i got
%%a was unexpected at this time
? -
SuicideSheep almost 10 yearsYes of course..Could it be there's some typo in between?
-
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 over 9 yearsWhen 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 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 over 8 years@AlexG, if you need the date (the original question didn't include it in the desired output), change the
echo
command toecho(!date! !time! !data!
-
allwynmasc over 8 yearskeeps 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 over 8 yearsYou 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 over 8 years@sabo-fx sometimes 'sleep 1 >NUL' not works(not recognized command), i used 'timeout 1 >NUL'
-
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 over 8 yearsno response on ping, so I did control+C and computer got slow, froze then blue screen
-
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
orping.cmd
? -
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 almost 8 years404 on that link now
-
mikia over 7 yearsI tested the script. Work fine, but you must add one space char in 3. line after set and before /p.
-
sabo-fx over 7 years@mika Thanx for your suggestion. I've corrected the code.
-
Serinus over 7 yearsWorked beautifully without any of the infinite loop/max cpu issues of the other suggestions.
-
Admin about 7 yearsPerdona, coloqué "un alias de servidor" coloca la direcciónIP en lugar de la cadena "sntdn", saludos
-
Jorge Cornejo Bellido about 7 yearsI have tried to add the -l 1024 parameter to the ping result but no luck... How to add another parameters? Thanks!
-
MC ND about 7 years@JorgeCornejoBellido, I've added it to the source ping before the
-t
without problems. -
Rhak Kahr over 6 yearsping 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 over 6 yearsit doesn't work anymore, it says "time" instead of the actual time
-
MC ND over 6 years@Mike, Tested now. After copy, paste into a batch file and execute (W10.0.15063 64b), it works.
-
Satish over 6 yearsThis is best so far
-
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 almost 6 yearsTest-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
-
Shoaib Khan almost 6 yearsWorks perfectlly!!
-
Otto medina almost 6 yearsWindows 7: This completely killed my machine. Kept starting new cmd.exe processe. Don't try this at home!
-
Otto medina almost 6 yearsThis does NOT work on Windows, as was asked in the question.
-
MC ND almost 6 years@Janos, How did you name your batch file?
-
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 over 5 yearsDoh! 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 over 5 yearsAs of September 8 2018, the posted code works perfectly for me on Windows 10 without changes. Thanks!
-
Sree Harsha over 5 yearssimple and crisp solution. first option worked for me
-
Lee over 5 yearsThis works just fine on Windows, under Windows Subsystem for Linux (WSL) which is an optional feature.
-
skomisa about 5 yearsThis solution is identical to one provided in an earlier answer to this question.
-
Himanshu Shekhar about 5 yearsI had been trying to log ping results.
date +%s
is more helpful as it shows integer timestamps. -
jorisw almost 5 yearsOn Mac,
-w
is an invalid option toping
. -
prem over 4 yearsIt's November 2019 and still it works perfectly without any modifications on Windows 10.
-
khalidmehmoodawan over 4 years@Janos : This works in Windows. Please use Windows PowerShell.
-
khalidmehmoodawan over 4 years@skomisa : it is a little different as it is also saving to file that can be later used.
-
mwfearnley over 4 yearsIf 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
-
flandersen almost 4 yearsOption 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 almost 4 yearsExtending 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 executeGet-Content C:\temp\ping.txt -tail 10 -wait
in another PowerShell. -
Keith E. Truesdell almost 4 yearsthis 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 over 3 yearsIf I use this batch and save the output to a txt file, does it record
request time outs
? -
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 over 3 yearsTried 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 over 3 years@MarcusNyberg, How did you name the batch file?
-
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 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 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 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.