1

I need to know when a device is showing certain messages. In order to do that, I need to store all the output in a log.txt file with the timestamp.

I use a telnet session from the cmd of my Windows PC, and I'd like to know if it's possible to store the output of the session prepending the timestamp at each line that is shown.

Basically, I open the cmd and type:

telnet 192.168.1.1

then I inpunt a command to show the logs of the device and it starts showing status messages at random times. Something like this:

[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
 

I need to know those times and record them somehow in a log.txt file, so I'd like to store something like this:

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

Is there any cmd command I could add to do that? I was also exploring the Teraterm cmd commands, but not sure how to use them.

Natiya
  • 463
  • 2
  • 9
  • 25

1 Answers1

0

You can pipe the output to a batch file, which reads from the pipe and prefix each line with a timestamp.

echo my telnet input | telnet 192.168.1.1 | addTimestamp.bat 

For an asynchronous pipe reading batch, take a look at SO: Read stdin line by line

You only need to modify

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

to

echo(!time! [!lineCnt!]: !line!
jeb
  • 78,592
  • 17
  • 171
  • 225
  • thank you! so I guess I need to put the telnet commands where the "ping localhost -n 2 > nul" is, right? and this would be the second script in that answer – Natiya Aug 24 '21 at 09:26
  • @Natiya No, don't modify the batch, the `ping localhost -n 2` is only a way to wait a second. See my edit, how to use pipes with your telnet command – jeb Aug 24 '21 at 09:35
  • thank you! but how do I enter the commands after I open the telnet session? Because, from the cmd, I open the telnet session to the device and then I put the command to show the logs I want to store with the timestamps. is that in the cmd after that line you put in your edit? – Natiya Aug 24 '21 at 10:55
  • @Natiya If you use `telnet 192.168.1.1 | addTimestamp.bat` from the command line, just type the commands, probably you can't see the telnet output at that point, but should work nevertheless – jeb Aug 24 '21 at 12:20