I can't sftp directly into a particular host. To move a file from my home machine to the host, I must sftp a file to an intermediate host; ssh into the intermediate host; and sftp the file to the final destination. Is it possible to avoid such madness?
|
feedback
|
migrated from stackoverflow.com Mar 26 '11 at 23:16
This question came from our site for professional and enthusiast programmers.
|
From your local machine you can create am SSH tunnel through the intermediate host to the final host:
This will open port 2000 on your localhost that will connect directly to the final server on port 22, by tunneling through the intermediate host. Now in another prompt connect with sftp on port 2000 to be tunneled through to the final server, noting that the user specified here is for the final host:
Seems like this belongs on superuser.com or serverfault.com though. | |||||
feedback
|
|
You can use SFTP's ProxyCommand option to transparently tunnel an SFTP connection over an SSH connection (a bit similar to WhiteFang34's answer, but over the SSH connection's stdin&stdout, rather than a forwarded local TCP port):
(That's assuming the intermediate host has netcat installed as /usr/bin/nc -- if not, you may have to find/install some equivalent way of gatewaying stdin&stdout into a TCP session.) What's really cool about this option is that you can add it to your ~/.ssh/config file, which makes it transparent:
With that entry, you can use sftp, scp, and ssh to finalhost, and it'll automatically invoke the tunnel. The only nontransparent part is that it'll prompt for two passwords (intermediatehost followed by finalhost), but if you want you can eliminate that as well with SSH keypairs... | |||||||||
feedback
|
|
You can pipe data to the ssh process running on your machine, then run a command on the intermediate machine which reads stdin and sends it to sftp as appropriate. This can be done in a oneliner on your local machine, though the quoting of arguments to ssh will require care. I am on my phone right now so unfortunately cannot type the details. Perhaps somebody else can complete this answer as an exercise! | |||
|
feedback
|
|
I'm assuming the final host is firewalled and I can only guess at methods you could use to go around it. For example - expose ssh from your local machine, then ssh to the first host, then ssh to the second and sftp from the final host to your machine. | |||
|
feedback
|
|
lets say A and B are the first and second hosts. And the file to be copied is foo Instead of sftp, you can use the following cat foo | ssh A "cat - > foo" Now, you can daisy-chain 2 of these together cat foo| ssh A "cat - | ssh B \"cat - > foo\" " | |||
feedback
|
