Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Use script to make a typescript of terminal session.

Then use sed to sanitise it.


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

share|improve this answer
I modified your ANSI Escape regex to start with a non-printable because it was stripping other things too. I think if you apply the change we have a solution: s/[^[:print:]][[[:digit:]]*(;[[:digit:]]*)*[[:alpha:]]//g; – Sean DeNigris Jun 3 '11 at 1:04
I think that regular expression is probably a little too loose, but if it works for you, it works for you! – Johnsyweb Jun 3 '11 at 1:11
Yeah, I was thinking the same thing, but I'll use it until it hurts :) – Sean DeNigris Jun 3 '11 at 1:37
Plus, I don't know what else to do because it doesn't work without it, and mac's sed doesn't seem to let you specify specific ASCII does in the regex. I really need to specify 027 (ESC), but it doesn't seem possible – Sean DeNigris Jun 3 '11 at 2:37
@Sean DeNigris: Hmmm... BSD's sed does seem to struggle with those. I've updated my command for you. – Johnsyweb Jun 3 '11 at 4:19

A very basic solution to clean the output would be the following:

sed 's/[\000-\037]^M/ /g' output.txt

I don't know however how to get that back into a file. For example, given the file

[~] # ls /
[1;34mbin[0m/         [1;36mhome[0m@        [1;34mlost+found[0m/  [1;36mphp.ini[0m@     [1;34msbin[0m/        [1;34msys[0m/         [1;34mvar[0m/
[1;34mdev[0m/         [1;34mlib[0m/         [1;34mmnt[0m/         [1;34mproc[0m/        [1;34mshare[0m/       [1;34mtmp[0m/
[1;34metc[0m/         [1;36mlinuxrc[0m@     [1;34mopt[0m/         [1;34mroot[0m/        [0;[0m  [1;36musr[0m@

this results in the following being output:

[~] # ls /
bin/         home@        lost+found/  php.ini@     sbin/        sys/         var/
dev/         lib/         mnt/         proc/        share/       tmp/
etc/         linuxrc@     opt/         root/  usr@

You can also log a session with script. So whenever you enter the SSH session, first type

script output.txt

which will then open a new shell and log all your actions without needing to redirect output or anything. When you're finished, just exit the shell. If the remove SSH server doesn't have script, you can run it from OS X, but then the SSH output will be garbled again.

share|improve this answer
I think that it only appears to be helping because it's viewed in the terminal, which correctly displays them. If I redirect your solution into a file, it contains the same mess. – Sean DeNigris Jun 3 '11 at 1:00
I know, that's why I said it wasn't perfectly working :) – slhck Jun 3 '11 at 7:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.