I want to copy files from a remote computer (W) to a private host computer (P) via scp. Since i can't connect to machine W directly i use an ssh tunnel over 2 different machines with port 22. What i would like to do, is to log into machine W and from there do an scp file transfer to machine P, the problem is that P is hidden in an network so i cant specify a remote host ip when i am logged into machine W. Is there a solution for this ?? I manage to do this when i am logged into machine P, but i dont want to remember the file path each time i copy a file.
After clarifying your setup as discussed above, your system looks like this:
P -- R -- M1 -- M2 -- W
R is a NAT router that only allows connections from
M1, but not the other way around and all nodes must be used in order to establish a connection.
Your only real problem is the router
R over which you have no control, so you must somehow establish a tunnel to allow
W to connect back. The simplest solution would be to have
P establish an SSH connection to
M1 and establish a reverse port forward there.
You can then set up a second SSH tunnel from
M2 to forward local packets to the reverse port forwarded port on
M1, which can in turn be used to establish a tunneled SSH connection from
Now this adds quite some complexity to the whole setup, which makes it quite error prone. A better solution would be to establish a VPN connection between
M2 and between
M2. Then you could establish a routing using a private address range from
P over these three tunnels. For this to work you need root access to both
M2 AND you need to set up proper firewalls so nobody from the outside gets into your VPN's.
There are of course other solutions as well, but what you need to think through how the packets are going to get through all these blockades, the actual solution can then be tailored to fit your tools.
I would recommend mounting the remote file system locally using
sshfs. I don't know the details of your setup but I'll give you an example. I set up a tunnel through a remote server (R1) to an inaccessible second server (R2) on R1's network with this command:
ssh -fN -p 24222 user@R1 -L 2222:R2:22
I can now connect to R2 like so:
ssh -p 2222 127.0.0.1
I can also use
sshfs to locally mount a directory from R2:
sshfs -p 2222 127.0.0.1:/path/to/dir /mnt/mountpoint
I have tried this using a tunnel through one not two remote servers but I see no reason why it should not work with 3 or more as long as the tunnel has been properly set up.
This is not exactly what you asked for but it should serve as a workaround. Once you have the remote file system mounted locally, you can use simple
cp to copy files to/from it.