The power of pipe and SSH

2014-12-09 16:59

The concept of pipes in Unix is very powerful. If we combine pipes with tar and ssh we have a simple and secure way of transfering files. It might not be the best way but these are tools that are available on almost any Unix system and it does have some advantages such as not using any space on the source system.

Simple file transfer

From a host you are currently logged on to:

$ tar zcf - source | ssh user@host 'tar zxf -'

From a remote host:

$ ssh user@host 'tar zcf - source' | tar zxf -

source in the above examples is a list of one or more directory and/or files. user is your username and host is the address of the serverto transfer to or from.

zcf is shorthand for --gzip, --create and --file. Using a single dash,-, as argument means 'use stdin or stdout'.

The first part of the first example can thus be read as 'create a compressed archive from source and send it to stdout'. We then use a pipe to connect the stdout of this command to the stdin of the command we run on the remote host using ssh. In effect we're creating a archive on the fly on one end and extracting it immediately at the other end.

The second example is just the same thing but in reverse.

More examples

If we make use of some other commands we can do even more.

Offsite backup from current host...

$ tar zcf - source | ssh user@host 'cat - > source-backup.tar.gz'

...or from remote host:

$ ssh user@host 'tar zcf - source' | cat - > source-backup.tar.gz

Create a drive mirror, for example if we booted a crasched machine from a LiveCD and needs to create a image for recovery or forensic:

$ dd if=/dev/sda | ssh user@host 'dd of=sda.img'

And the other way around, for example to clone a disc:

$ ssh user@host 'dd if=sda.img' | dd of=/dev/sda

Note: Always be careful with dd since it's a very powerful and possibly destructive tool.