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?


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.

  • 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
  • 1
    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
  • 1
    @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;0mstunnel.pid[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/        stunnel.pid  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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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