在 Windows CLI 上使用时间戳 Ping [英] Ping with timestamp on Windows CLI

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

问题描述

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

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 编辑:在更快/更新的机器/操作系统版本中,以前的代码存在同步问题,使得 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 命令包含在子 shell 的开头(只能使用一个,但是由于 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

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

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