使用时间戳记Ping [英] Ping with timestamp

查看:218
本文介绍了使用时间戳记Ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows命令提示符 cmd ,我使用 ping -t到10.21.11.81 / p>

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

请注意,我只想使用CMD提供的命令

Please note that I wanna achieve this with only commands provided by CMD

推荐答案

@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!)" 

在批处理文件中使用。要使用命令行将 %% a 替换为%a

note: code to be used inside a batch file. To use from command line replace %%a with %a

启动ping,强制正确的行缓冲输出( find / v ),并启动 cmd 启用延迟扩展,这将执行无限循环读取管道数据,将被回传到以当前时间为前缀的控制台。

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 编辑
在较快/较新的机器/ os版本中,在以前的代码中存在同步问题,使得 set / p ping 命令仍然在写它,并且结果是换行符。

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"

子$开头包含两个暂停命令(只能使用一个,但是 pause 使用一个输入字符,一个CRLF对被断开,一个带有LF的行被读取)等待输入数据,并且 ping -n 2 localhost 包含在内循环中的每次读取等待一秒。结果是更稳定的行为和更少的CPU使用。

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.

注意:内部 ping pause ,但每个读取的行的第一个字符由 pause 使用,而不是由 set / p

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

这篇关于使用时间戳记Ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆