Sui*_*eep 69 timestamp cmd ping
在Windows命令提示符下cmd,我使用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
Run Code Online (Sandbox Code Playgroud)
有没有可能获得这样的输出?
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
Run Code Online (Sandbox Code Playgroud)
请注意,我想通过CMD提供的命令实现这一目标
MC *_* ND 90
@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!)"
Run Code Online (Sandbox Code Playgroud)
注意:要在批处理文件中使用的代码.从命令行替换%%a使用%a
启动ping,强制使用正确的行缓冲输出(find /v),并启动一个cmd启用延迟扩展的进程,该进程将执行无限循环读取管道数据,该数据将回显到以当前时间为前缀的控制台.
2015-01-08 编辑:在更快/更新的机器/操作系统版本中,以前的代码中存在同步问题,set /p在ping命令仍在写入时使读取成行,结果是行切割.
@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"
Run Code Online (Sandbox Code Playgroud)
pause在子shell的开头包含两个附加命令(只能使用一个,但是为了pause消耗输入字符,CRLF对被破坏并且带有LF的行被重新加入)以等待输入数据,并且ping -n 2 localhost包括等待内循环中的每次读取一秒钟.结果是更稳定的行为和更少的CPU使用率.
注意:内部ping可以用a替换pause,但是每个readed行的第一个字符由the消耗pause而不是由set /p
小智 62
WindowsPowershell:
选项1
ping.exe -t COMPUTERNAME|Foreach{"{0} - {1}" -f (Get-Date),$_}
Run Code Online (Sandbox Code Playgroud)
选项2
Test-Connection -Count 9999 -ComputerName COMPUTERNAME | Format-Table @{Name='TimeStamp';Expression={Get-Date}},Address,ProtocolAddress,ResponseTime
Run Code Online (Sandbox Code Playgroud)
Sam*_*ley 43
你可以这样做:
ping 10.0.0.1 | while read line; do echo `date` - $line; done
Run Code Online (Sandbox Code Playgroud)
虽然它没有给出当你最后点击^ C时通常得到的统计数据.
sab*_*-fx 28
批处理脚本:
@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)
Run Code Online (Sandbox Code Playgroud)
此脚本将询问要ping的主机.Ping输出输出到屏幕和日志文件.示例日志文件输出:
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
Run Code Online (Sandbox Code Playgroud)
日志文件名为LOG_ [hostname] .log,并写入与脚本相同的文件夹.
小智 7
这可能适合更高版本的 Windows 版本:
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:
Run Code Online (Sandbox Code Playgroud)
您可以使用其他答案之一。
while :;do ping -n -w1 -W1 -c1 10.21.11.81| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done
Run Code Online (Sandbox Code Playgroud)
或作为pingt〜/ .bashrc的函数:
pingt() {
while :;do ping -n -w1 -W1 -c1 $1| grep -E "rtt|100%"| sed -e "s/^/`date` /g"; sleep 1; done
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //stackoverflow.com/a/26666549/1069083
我认为我的代码是每个人都需要的:
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"
Run Code Online (Sandbox Code Playgroud)
显示:
[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
Run Code Online (Sandbox Code Playgroud)
注意:要在命令行中使用的代码,并且您必须在操作系统上预安装 powershell。
这可能对某人有帮助:[需要在Windows PowerShell中运行]
ping.exe -t 10.227.23.241 |Foreach{"{0} - {1}" -f (Get-Date),$_} >> Ping_IP.txt
Run Code Online (Sandbox Code Playgroud)
-检查当前目录或用户主目录中的Ping_IP.txt文件。
上面的命令给你输出如下文件;
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
Run Code Online (Sandbox Code Playgroud)