How to save console output still showing it

Posted on

Problem :

I need to troubleshoot a telnet session and send save it’s results to another person. When I use redirection I save the contents of my session, but I’m not able to see it on console.

How to pipe current terminal/program and still show it on current console.

When I use the command below I can save the program’s output, but I’m not able to see it on console.

telnet ADDRESS PORT >> myoutputfile

I could use a telnet output option, I’m looking for a command independent way to do it.

The question is specific for a telnet session, but applies to any other command, including those that don’t have a “save output to file option”.

Solution :

The simplest solution is to use tee:-

telnet ADDRESS PORT | tee -a myoutputfile

This copies output to both standard output and the specified file name, and the -a option appends, just as your >> redirection did.

There’s a trick to use the error output as a second buffer to output to 2 devices.

Your command would be:

telnet ADDRESS PORT >&2 2>>myoutputfile

This appends the output to myoutputfile and also writes whatever comes out to console.

>&2: This copies output from stream 1 (console) to stream 2 (error)

2>>: This makes sure everything from stream 2 is being sent to the file.

If you do not want to risk having errorlogging added to the file, change the 2 to 3 in both rows. (>&3 3>>file)

EDIT: I just noticed the linux tag. This is written for Windows, but it might still work.

Leave a Reply

Your email address will not be published. Required fields are marked *