【问题标题】:adding timestamp to each line of a telnet session log from Windows cmd从 Windows cmd 向 telnet 会话日志的每一行添加时间戳
【发布时间】:2021-10-24 09:48:12
【问题描述】:

我需要知道设备何时显示某些消息。为此,我需要将所有输出与时间戳一起存储在 log.txt 文件中。

我从我的 Windows PC 的 cmd 使用 telnet 会话,我想知道是否可以在显示的每一行的时间戳前面存储会话的输出。

基本上,我打开 cmd 并输入:

telnet 192.168.1.1

然后我输入一个命令来显示设备的日志,它开始随机显示状态消息。像这样的:

[OL 225451]lib_thread_show:304 [0]name=decode, pid=0x6014f6, st=1
[OL 225451]lib_thread_show:304 [1]name=handler, pid=0x601df6, st=1
[OL 225451]lib_thread_show:304 [2]name=notify_handler, pid=0x6014b9f6, st=1
[OL 225451]lib_thread_show:304 [3]name=event_handler, pid=0x601f6, st=1

[OL 225451]lib_thread_show:304 [5]name=msghandler, pid=0x60f41bf6, st=1
[OL 225451]lib_thread_show:304 [6]name=service, pid=0x60f417f6, st=1
[OL 225451]lib_thread_show:304 [7]name=service, pid=0x60413f6, st=1
[OL 225451]lib_thread_show:304 [8]name=, pid=0x00000000, st=0
 

我需要知道这些时间并以某种方式将它们记录在 log.txt 文件中,所以我想存储如下内容:

2021/08/13 13:35:03[OL 225451]lib_thread_show:304 [0]name=decode, pid=0x6014f6, st=1
2021/08/13 13:35:04[OL 225451]lib_thread_show:304 [1]name=handler, pid=0x601df6, st=1
2021/08/13 13:35:06[OL 225451]lib_thread_show:304 [2]name=notify_handler, pid=0x6014b9f6, st=1
2021/08/13 13:35:08[OL 225451]lib_thread_show:304 [3]name=event_handler, pid=0x601f6, st=1

2021/08/13 13:36:03[OL 225451]lib_thread_show:304 [5]name=msghandler, pid=0x60f41bf6, st=1
2021/08/13 13:36:08[OL 225451]lib_thread_show:304 [6]name=service, pid=0x60f417f6, st=1
2021/08/13 13:36:09[OL 225451]lib_thread_show:304 [7]name=service, pid=0x60413f6, st=1
2021/08/13 13:36:13[OL 225451]lib_thread_show:304 [8]name=, pid=0x00000000, st=0

我可以添加任何 cmd 命令吗?我也在探索Teraterm cmd 命令,但不知道如何使用它们。

【问题讨论】:

    标签: windows batch-file cmd timestamp telnet


    【解决方案1】:

    您可以将输出通过管道传输到批处理文件,该文件从管道中读取并在每行前面加上时间戳。

    echo my telnet input | telnet 192.168.1.1 | addTimestamp.bat 
    

    对于异步管道读取批处理,请查看 SO: Read stdin line by line

    你只需要修改

    echo( READ[!lineCnt!]: !line!
    

    echo(!time! [!lineCnt!]: !line!
    

    【讨论】:

    • 谢谢!所以我想我需要将 telnet 命令放在“ping localhost -n 2 > nul”所在的位置,对吗?这将是该答案中的第二个脚本
    • @Natiya 不,不要修改批次,ping localhost -n 2 只是等待一秒钟的一种方式。查看我的编辑,如何在 telnet 命令中使用管道
    • 谢谢!但是打开 telnet 会话后如何输入命令?因为,从 cmd,我打开到设备的 telnet 会话,然后我输入命令来显示我想要存储的带有时间戳的日志。是在你编辑的那一行之后的 cmd 中吗?
    • @Natiya 如果您在命令行中使用telnet 192.168.1.1 | addTimestamp.bat,只需键入命令,此时您可能看不到 telnet 输出,但应该可以正常工作
    猜你喜欢
    • 1970-01-01
    • 2015-08-30
    • 2017-04-07
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多