14

There are two server that I can access with 2 different VPN connections. I have managed to have both VPN working on the same time on my machine (a bit of routing rules).

I want to do a scp <remote1>:some/file <remote2>:destination/folder from my laptop terminal. But when I try this, the scp command that is invoked on remote1 cannot find remote2 because they are not in the same network. Is it possible to force the scp command to pass through my laptop as a router?

If I try with Nautilus (connect to server, both servers, then copy-paste) it works, but I'd like to do it from a terminal.

5
  • It's your machine that contacts both, remote1 and remote2. Are you sure the error is that remote1 can't access remote2?
    – Jan
    Commented Sep 22, 2014 at 9:44
  • Yes, the error is ssh: Could not resolve hostname <remote2>: Name or service not known lost connection
    – Danduk82
    Commented Sep 22, 2014 at 9:45
  • And if I try to use the IP address directly for <remote2> the connection fails after a while (hangout).
    – Danduk82
    Commented Sep 22, 2014 at 9:47
  • Then I guess it's your machine that can't connect to remote2. Perhaps the VPN connection times out or is flakey...
    – Jan
    Commented Sep 22, 2014 at 9:54
  • No it works, even the DNS is working. I really have the feeling that the scp command tries to execute the connection between remote1 and remote2 directly on remote1.
    – Danduk82
    Commented Sep 22, 2014 at 9:57

3 Answers 3

35

Newer versions of scp have the option -3

-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
2
  • great! Exactly what I needed. Thanks a lot
    – Danduk82
    Commented Sep 22, 2014 at 9:58
  • 1
    Great find, I didn't know that!
    – Jan
    Commented Sep 22, 2014 at 10:02
10

If you need more flexibility than scp -3, plain ssh and pipes are fun.

This is equivalent to redirecting the stream over the local machine:

ssh sourceRemote 'cat /path/to/sourceFile' \
| ssh destinationRemote 'cat > /path/to/destinationFile'

You can then add more stream redirections at any point, to e.g. chain several intermediate machines, or save a local copy of the transferred file as it's being routed.

1
  • 2
    You'll get a nice progress bar if the source machine has pv installed and you replace the first cat with it.
    – liori
    Commented Sep 22, 2014 at 23:20
7

Expanding on @Anko's answer, you can use tar for multiple files:

ssh -q user@sourcehost 'cd /source/path; tar -czf - files...' | \
ssh -q user@desthost 'cd /dest/path; tar -xzf -'

Compressing is entirely optional - your data may not be very compressible or big enough to warrant the overhead. If your version of tar doesn't support the -z switch, pipe through gzip -c and gzip -dc respectively.

PS: Beware versions of tar that don't strip leading slashes on extraction (i.e., write to absolute pathnames if supplied).

Edit: -e none isn't needed with pipes, because a pseudo-terminal isn't allocated.

2
  • 1
    ssh has a -C flag to enable stream compression. According to the man-page, it uses the same algorithm as gzip though. The manual page also helpfully elucidates when to use it: "Compression is desirable on modem lines and other slow connections, but will only slow things down on fast networks."
    – Anko
    Commented Sep 22, 2014 at 19:49
  • 4
    @Anko, the problem with using -C here is that the local machine would have to uncompress and recompress the data. Telling tar to do the compression means that the compressed data is passed straight through the pipe without any processing.
    – cjm
    Commented Sep 23, 2014 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .