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:
tar
on A
collecting data and piping that to the SSH server on A
- The SSH server on
A
sends the output on to the first ssh
command via Unix Sockets
- The first ssh command pipes (or streams) everything to it's
stdout
where the |
(pipe) command picks it up.
- The pipe command passes the output of the first
ssh
to the second.
- The second
ssh
sends the data to the SSH server on B
via Unix Sockets
- 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.