124

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

2

19 Answers 19

153

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
7
  • 4
    Worked beautifully without any of the infinite loop/max cpu issues of the other suggestions.
    – Serinus
    Nov 29, 2016 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 1, 2018 at 15:28
  • simple and crisp solution. first option worked for me Oct 22, 2018 at 12:40
  • 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, 2020 at 7: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, 2020 at 7:39
112
@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

24
  • 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, 2014 at 9: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 8, 2015 at 12:24
  • 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 1, 2015 at 5:42
  • 4
    no response on ping, so I did control+C and computer got slow, froze then blue screen
    – Dan
    Nov 8, 2015 at 4: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 8, 2015 at 8:44
78

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.

7
  • 4
    This is best so far
    – Satish
    Nov 7, 2017 at 20:15
  • 2
    Works perfectlly!! Jun 23, 2018 at 8:01
  • 7
    This does NOT work on Windows, as was asked in the question.
    – Janos
    Jul 19, 2018 at 11:26
  • 4
    This works just fine on Windows, under Windows Subsystem for Linux (WSL) which is an optional feature.
    – Lee
    Nov 3, 2018 at 15:12
  • 2
    -D Print timestamp (unix time + microseconds as in gettimeofday) before each line. You can update this, ping now comes built-in with this functionality.
    – NessDan
    Feb 7, 2022 at 19:38
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.

11
  • 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, 2015 at 8: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, 2015 at 10:19
  • @sabo-fx sometimes 'sleep 1 >NUL' not works(not recognized command), i used 'timeout 1 >NUL'
    – Asprelis
    Oct 22, 2015 at 9: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, 2015 at 1:37
  • 1
    It's July 2022 and still it is working perfectly without any modifications on Windows 10. Jul 17, 2022 at 17:57
27

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 !!!

2
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:
2
  • 1
    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 May 14, 2020 at 15:04
  • is this time from server or from client? Jul 3, 2023 at 4:26
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

1
  • 1
    On Mac, -w is an invalid option to ping.
    – jorisw
    Jun 12, 2019 at 6:23
5

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.

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

1
  • 404 on that link now
    – Hicsy
    Jun 23, 2016 at 0:38
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
0
2
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"
2
  • please make sure the edited content is correct
    – mozway
    Sep 6, 2023 at 10:27
  • While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – Yunnosch
    Sep 6, 2023 at 10:28
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/

0
1

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
1

Try this instead:

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

Check if it suits you

1
  • 1
    Perdona, coloqué "un alias de servidor" coloca la direcciónIP en lugar de la cadena "sntdn", saludos
    – JuanZR
    Mar 14, 2017 at 17:35
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.

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

Try this on windows powershell. just change 'ADDRESS' to either an IP or a URL(e.g google.com, facebook.com)

ping.exe -t ADDRESS|Foreach{"{0} - {1}" -f (Get-Date),$_}
2
  • how is this different from this answer?
    – Stephan
    Jan 29 at 8:28
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 4 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.