Piping Output Over A Secure Shell (SSH) Connection

14 Oct 2009
Posted by Jeet Sukumaran

We all know about using scp to transfer files over a secure shell connection. It works fine, but there are many cases where alternate modalities of usage are required, for example, when dealing when you want to transfer the output of one program directly to be stored on a remote machine. Here are some ways of going about doing this.

Let "$PROG" be a program that writes data to the standard output stream. Then:

  • Transfering without compression:
    $PROG | ssh destination.ip.address 'cat > ~/file.txt'
  • Using gzip for compression:
    $PROG | gzip -f | ssh destination.ip.address 'gunzip > ~/file.txt'
  • Better compression can usually be achieved by bzip2
    $PROG | bzip2  | ssh destination.ip.address 'bunzip2 > ~/Scratch/file.txt'

I find this useful enough to source the following function into all my shells:

## xof #######################################################################
# Pipe from standard input to remote file.
xof() {
    if [[ -z $1 || -z $2 ]]
    then
        echo usage: '<STDOUT> | xof <DESTINATION IP/ADDRESS> <DESTINATION FILE PATH>'
        echo
        echo Pipe standard input to remote file.
    else
        bzip2 | ssh $1 'bunzip2 > '$2
    fi
}

And when I want to get fancy, I pipe the output directly to my favorite text editor, BBEdit:

## xobb #######################################################################
# Pipe from standard input to bbedit.
xobb() {
    if [[ -z $1 ]]
    then
        echo usage: '<STDOUT> | xobb <DESTINATION IP/ADDRESS>'
        echo
        echo Pipe standard input to BBEdit.
    else
        bzip2 | ssh $1 'bunzip2 | bbedit'
    fi
}

On a tangential note, if you have a large number of files that you want to transfer, the following is more efficient than separately tar-ing and scp-ing:

tar cf - *.t | ssh destination.ip.address "tar xf - -C /home/jeet/projects/bbi2
Tags:

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a biological visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.