Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

Thanks in advance

share|improve this question
    
It's your machine that contacts both, remote1 and remote2. Are you sure the error is that remote1 can't access remote2? –  Jan 7 hours ago
    
Yes, the error is ssh: Could not resolve hostname <remote2>: Name or service not known lost connection –  Danduk82 7 hours ago
    
And if I try to use the IP address directly for <remote2> the connection fails after a while (hangout). –  Danduk82 7 hours ago
    
Then I guess it's your machine that can't connect to remote2. Perhaps the VPN connection times out or is flakey... –  Jan 7 hours ago
    
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 7 hours ago

3 Answers 3

up vote 16 down vote accepted

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
share|improve this answer
    
great! Exactly what I needed. Thanks a lot –  Danduk82 7 hours ago
1  
Great find, I didn't know that! –  Jan 7 hours ago

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 -e none 'cat /path/to/sourceFile' \
| ssh destinationRemote -e none `cat > /path/to/destinationFile'

-e none disables ssh's escape character, so the connection won't accidentally be killed by arbitrary data in the stream.

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

share|improve this answer

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

ssh -q -e none user@sourcehost 'cd /source/path; tar -czf - files...' | \
ssh -q -e none 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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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