sábado, 17 de junio de 2017

Copy file with SCP via SSH tunnel

If we want to copy a file to remote machine R, but this machine is only accessible from a gate machine  G.  The idea is something like this:



Step 1: We set the forwarding port:

ssh -L <port>:<R address>:22 <G user>@<G address>

Step 2: Run scp against <port> pretending 127.0.0.1 (localhost) is the remote machine, and the command will be sent to R.

For copy file from local machine to remote machine:


scp -P <port> path-file-name-to-be-copied <R user>@127.0.0.1:/path/to/file
For copy file from remote machine to local machine:


scp -P <port> <R user>@127.0.0.1:/path/to/file path-file-name-to-be-copied
Reference:
https://www.urbaninsight.com/2012/10/03/running-scp-through-ssh-tunnel
http://whoochee.blogspot.com/2012/07/scp-via-ssh-tunnel.html
http://www.rzg.mpg.de/networkservices/ssh-tunnelling-port-forwarding