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.

Braiam
  • 35,991
Danduk82
  • 482
  • It's your machine that contacts both, remote1 and remote2. Are you sure the error is that remote1 can't access remote2? – Jan Sep 22 '14 at 09:44
  • Yes, the error is ssh: Could not resolve hostname : Name or service not known lost connection – Danduk82 Sep 22 '14 at 09:45
  • And if I try to use the IP address directly for the connection fails after a while (hangout). – Danduk82 Sep 22 '14 at 09:47
  • Then I guess it's your machine that can't connect to remote2. Perhaps the VPN connection times out or is flakey... – Jan Sep 22 '14 at 09: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 Sep 22 '14 at 09:57

3 Answers3

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
Andy
  • 602
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.

Anko
  • 4,526
  • 2
    You'll get a nice progress bar if the source machine has pv installed and you replace the first cat with it. – liori Sep 22 '14 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.

arielCo
  • 1,058
  • 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 Sep 22 '14 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 Sep 23 '14 at 16:05