7

Let’s say we have three machines: A, B and C.

  • Machine A can't reach machine B from any network, so I can't send files between both.
  • But both A and B can be reached from C (my machine).

Today I have to copy a huge file from A to B.

Currently I would need to copy it first from A to C and then from C to B. Is there a way to connect or pipe the scp to stream the incoming data to the target machine without need to save in the middle?

AdminBee
  • 22,803
Magno C
  • 255
  • 1
  • 2
  • 9

2 Answers2

16

man scp writes (with my emphasis)

-3 Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. [...]

So what you need is a copy command that routes via your local system (I almost always include -p, to preserve timestamps and permissions, so I've done that here too):

scp -3p serverA:path/to/file(s) serverB:/path/to/destination

Very recent versions of scp have -3 enabled by default since 9 August 2021, and to disable it one must now specify -R. Using this third party copy feature will disable prompts for passphrases and passwords as scp cannot ask for them for both hosts.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
1

scp user1@A:from_file user2@B:to_file should do the trick. Read man scp.

waltinator
  • 4,865
  • 1
    It expands to ssh user1@A "scp from_file user2@B:to_file" – Vlad Havriuk Aug 26 '21 at 19:24
  • 2
    @Vlad I think your tip will make A send file to B but as I said there is no network between both – Magno C Aug 27 '21 at 02:50
  • @MagnoC I know, this tip expands the answer of waltinator. – Vlad Havriuk Aug 27 '21 at 07:20
  • The whole point of the question seems to be that this method doesn't work. – Barmar Aug 27 '21 at 13:54
  • 2
    However, as mentioned in the other answer, recent versions of scp use -3 mode by default, in which case this should work. – Barmar Aug 27 '21 at 13:55
  • @Barmar as I can see this method (not this answer... I'm refer to Vlad's tip above) connect into "A" terminal (SSH) and then issue a SCP to "B" (using its location scope) what AFAK will not work because "A" can't reach "B" directly as I pointed in my question. Please correct my thought. This answer is correct since it respect the "-3" condition. – Magno C Aug 27 '21 at 16:22