How to save console output still showing it

3

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".

Rafael Borja

Posted 2014-12-02T20:15:14.407

Reputation: 155

Answers

7

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.

AFH

Posted 2014-12-02T20:15:14.407

Reputation: 15 470

A small issue: the ^] sequence (scape character) doesn't work when using tee. – Rafael Borja – 2014-12-02T21:57:36.200

1That's probably because telnet makes that transformation in its output when it's writing to stdout, which it isn't in this case. You could try appending | cat -v to the command, but this will display lines only when there is a new-line in the output. – AFH – 2014-12-02T22:28:46.197

1

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.

LPChip

Posted 2014-12-02T20:15:14.407

Reputation: 42 190

It makes sense, but doesn't work with a telnet session (I type the telnet commands, but I can't read the response) – Rafael Borja – 2014-12-02T20:42:38.570

1>&2 redirects output: it doesn't copy it. Your command has the same effect as the questioner's, except that error messages also go to the file instead of being displayed on the console. – AFH – 2014-12-02T20:48:20.350

@AFH on windows the & means copy the stream. I tested it and on windows it does work. – LPChip – 2014-12-02T21:15:13.573

Sorry, I'm so used to Linux syntax these days. – AFH – 2014-12-02T21:27:02.707