Ping with timestamp on Windows CLI
@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
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