익명 00:58

scp files via intermediate host

scp files via intermediate host

I have access to 3 machines, A, B, and C. The only possible (ssh) connections are:

A -> B
B <-> C

I need to get files from A to C, so I could scp the files from A to B, and then scp them from B to C. However, B doesn't have much disk space, so this is not an option. Is there a way to scp files from A to C via B? Note, I don't have root access on any of the machines, so don't think I can set up any persistent tunnels, but correct me if I'm wrong!



Top Answer/Comment:

ProxyJump

New in OpenSSH 7.3:

A$ scp -oProxyJump=B thefile C:destination

(Behind the scenes, this just uses ProxyCommand and ssh -W.)

ProxyCommand

Updated to include -W from other answers:

A$ scp -oProxyCommand="ssh -W %h:%p B" thefile C:destination

If A has a very old SSH client installed (without -W support), or if B is configured to disallow TCP forwarding (but still allows shell commands), use alternatives:

A$ scp -oProxyCommand="ssh B socat stdio tcp:%h:%p" thefile C:destination
A$ scp -oProxyCommand="ssh B nc %h %p" thefile C:destination

Pipes

A$ tar cf - thefile anotherfile | ssh B "ssh C \"cd destination && tar xvf -\""
A$ (echo thefile; echo anotherfile) | cpio -o | ssh B "ssh C \"cd destination && cpio -i\""

For just one file:

A$ ssh B "ssh C \"cd destination && cat > thefile\"" < thefile

"Tunnel" through B

A$ ssh -f -N -L 4567:C:22 B
(continues running in background)

A$ scp -P 4567 thefile localhost:destinationPath

When you're done, don't forget to kill the previously started ssh process (which has dropped to background due to -f -N).

  • -f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n.
  • -N Do not execute a remote command. This is useful for just forwarding ports.

Reverse "tunnel" through B to A

Doesn't always work though:

A$ ssh -f -N -R 4567:localhost:22 B
(now you can reach A from B, by using localhost:4567)

B$ scp -P 4567 localhost:thefile C:destination
  • -R Specifies that connections to the given TCP port or Unix socket on the remote (server) host are to be forwarded to the given host and port, or Unix socket, on the local side.
상단 광고의 [X] 버튼을 누르면 내용이 보입니다