scp between two remote hosts from my (third) pc

Solution 1:

In the past, the way in which scp worked, when called (naively) to copy files between remote systems, was very inconvenient: if you wrote, for instance

    scp user1@remote1:/home/user1/file1.txt user2@remote2:/home/user2/file1.txt

scp would first open an ssh session on remote1, and then it would run scp from there to remote2. For this to work, you would have to set up the authorization credentials for remote2 on remote1.

The modern way to do it, instead, ("modern" because it was implemented only a few years ago, and perhaps not everybody has a -3-capable scp) requires two steps.  The first necessary step is to use ~/.ssh/config to set up all options for the connection to both remote1 and remote2, as follows:

    Host remote1.example.org
    Port 2222
    IdentityFile /path/to/host1-id_rsa

    Host remote2.example.org
    Port 6969
    IdentityFile /path/to/host2-id_rsa

This way it becomes possible to pass all necessary options to the command without ambiguities: for instance, if we had said on the CLI use port 2222 without the above configuration, it would have been unclear whether we were referring to remote1 or to remote2, and likewise for the file containing the cryptgraphic keys. This way the CLI remains tidy and simple.

Secondly, use the -3 option, as follows:

    scp -3 user1@remote1:/home/user1/file1.txt user2@remote2:/home/user2/file1.txt

The -3 option instructs scp to route traffic through the PC on which the command is issued, even though it is a 3rd party to the transfer. This way, authorization credentials must reside only on the issuing PC, the third party.

Solution 2:

The source and target can be specified as a URI in the form scp://[user@]host[:port][/path]

so you can run:

scp -3 scp://[email protected]:22/path/to/file scp://[email protected]:6969/path/to/file

Solution 3:

Last time I tried this, scp wasn't able to do that. Your command line looks okay. This workaround will work:

ssh -p port_on_machine1 user@machine1 "cat /path/to/file/one"|ssh -p port_on_machine2 user@machine2 "cat >/path/to/file/two"