Ping with timestamp

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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

回答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



回答2:

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.

Kind regards, Joost Kuin - Quanza Engineering

my linkedin



回答3:

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


回答4:

You can just do this:

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.



回答5:

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



回答6:

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.



回答7:

Try this insted:

ping -c2 -s16 sntdn | awk '{print NR " | " strftime("%Y-%m-%d_%H:%M:%S") " | " $0 }' A VER QUE TE PARECE, OJALA Y TE SIRVA



回答8:

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


回答9:

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/



回答10:

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

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



转载请标明出处:Ping with timestamp
文章来源: Ping with timestamp
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!