0

I need to copy data from host A to host C. There is no connection between them. Only way is A <-> B and then B <-> C. To made this in some faster way I use the following command on B (the middle server):

    ania@hostname_B: /home/ania/
    ssh hostname_A 'cd /home/ania/lot_of_files; tar cf - .' | ssh hostname_B 'cd /home/ania/new_place; tar xf -'

It works, but I don't understand something here and really would like to. Where is hidden in this command is the strictly copying data?

I see normal ssh connection, change the directory and packing/unpacking files only done on two hostnames and after another separated by pipe. No files after execution are stored on hostname_B, there is no scp here used also. Done on AIX, but it will work on Linux also.

Ania
  • 3
  • 1

2 Answers2

2

foo | bar means "take all normal output from foo and pass it as input to bar" ("normal" since there is also "error" output).

It doesn't really matter what foo or bar are as long as the first one writes it output to stdout (a.k.a "file handle 1") and the second one can read from stdin (or file handle 0).

SSH does something similar but involving sockets. So it connects to the SSH server on a different computer (via Unix Sockets) and redirects stdin/out/err of the command running on the remote computer to the stdin/out/err of the ssh command which you executed locally.

So if you do ssh user@host echo Hello, then SSH will send the command echo Hello to the remote SSH server which will execute it "locally" (i.e. on the same computer where the SSH server is running). The result (Hello) will be piped through the SSH server into the socket connection to the computer running the ssh command which has set up its pipes to redirect the data to it's stdout.

So we have:

  1. tar on A collecting data and piping that to the SSH server on A
  2. The SSH server on A sends the output on to the first ssh command via Unix Sockets
  3. The first ssh command pipes (or streams) everything to it's stdout where the | (pipe) command picks it up.
  4. The pipe command passes the output of the first ssh to the second.
  5. The second ssh sends the data to the SSH server on B via Unix Sockets
  6. The remote SSH server on B pipes the data then to the second tar command as stdin

Or as a graphics:

tar cf --pipe>> SSH(A) --socket>> ssh hostA --pipe>> ssh hostB --socket>> SSH(b) --pipe>> tar xf

Where --pipe>> is a pipe between processes on the same computer and --socket>> is a Unix socket connection between two different computers.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Well they are not seperated by the pipe, but joined with the pipe. Its basicly B connects to A and tars the files and streams the file to host C where it executes the tar xf so it unpacks the files.

Maximilian Kindshofer
  • 2,753
  • 3
  • 22
  • 37