Linux – Capturing remote output locally in Mac Terminal

linuxmacsshterminal.app

I want local programmatic access to ssh output in Mac Terminal.

First, I tried redirecting the output of each command to a file. The file was perfect, but of course it was on the remote server, and an sftp for each command output seemed a little… heavy.

Next, I tried to Applescript Terminal, but it only gives access to the currently visible text in a tab (i.e. if half the output has already scrolled out of sight, it doesn't get returned – useless).

Last, I tried piping ssh to tee (e.g. ssh user@host | tee output.txt). This almost worked. I have the output in a local file, but there are a lot of unwanted characters mixed in. For example, every time I hit backspace, there's a ^H in the file. There's also text like "[0m[K" which is harder to get rid of.

How do I cleanly get this ssh output locally?

Best Answer

Use script to make a typescript of terminal session.

Then use sed to sanitise it.

E.g;

% script mysession

% ssh user@host

[some stuff]

% exit
Script done, output file is mysession

% sed -e '
s/'`echo "\033"`'\[[[:digit:]]*\(;[[:digit:]]*\)*[[:alpha:]]//g; # ANSI Escape sequences (perhaps over-generalised)
s/[^[:print:]]//g; # Non-printable
' mysession > mysession.txt

You should now be able to read mysession.txt without the ANSI escape codes and other non-printable characters.

This could be enhanced to delete the character before a ^H, et cetera, but you specified that you didn't want that.

Related Question