I need to download a file from machine C to machine A. To do that, I need to login to machine B (I am doing this using scp), as I cannot access C directly from A.

Would it be possible to download a file from C to A with a single command or a script?

  • 2
    I guess you are using ssh to login to machine B not scp. – Thushi Jul 10 '14 at 11:24

If you can access from machine B to A & C,

Newer version of scp support the -3 switch, which allow you copy a file between 2 remote machines.

 -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.  Note that this option disables the
         progress meter.

$ scp -3 user1@C:/file user2@A:/file
  • 1
    Didn't you forget to add the '-3' in the example command? Also, the question mentions C -> A, not A -> C. – Fox Jul 10 '14 at 13:34

If you have configured SSH-access on all machines, you can setup SSH-tunnel via machine B.
First step:

[user@A ~]$ ssh -f -L LOCALPORT:IP_ADDR_C:22 user_at_B@IP_ADDR_B

Key -f put ssh to background just before command execution. Good idea to use -N key.
From man ssh:

-N      Do not execute a remote command.  This is useful for just forwarding ports (protocol version 2 only).

Now you can use scp:

[user@A ~]$ scp -P LOCALPORT user_at_C@localhost:<your_file_at_C> <local_file>

For example, we'll download file test.txt from user's me homedir at machine placed behind machine host.example.com:

ssh -f -N -L 2222: me@host.example.com
scp -P 2222 me@localhost:~/test.txt .

There is a very simple way to do this!

  1. First connect to the gateway:

    ssh user@B
  2. Launch the copy C -> A

    ssh user@C "dd if=/path/source/file" | ssh user@A "dd of=/path/destination/file"

If you want to get rid of the messages written by dd on stderr, either use the option status=none if your version of dd supports this, or use 2> /dev/null.

Note that there is a bug in the version 8.4 of dd causing status=none not to work even if this is present in the documentation.

  • But this is only useful when the scp version on the gateway doesn't support the '-3' option of scp (Rabin's answer). – Fox Jul 10 '14 at 13:09

You can establish an ssh tunnel from B to C, then from A scp to B's port, where the tunnel serves to download the file. There are many information pages, Google for them, one of the first searches took me here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.