Normally there is an SSH client that connects to an SSH server. The client then places commands that are executed on the server. Now, is there a way to do it the other way around? So that the client connects to the server, but the server also connects to the client and is able to execute commands on the client. Is that possible?
The reason I am looking for this is that I have two servers (A and B) in two different networks, both networks connected to the Internet. Server A can be accessed through port forwarding, but server B cannot. Since server A does all the work I'd like server B to connect to server A and just do whatever server A wants it to do.
Are reverse connections like this possible? Or maybe bidirectional SSH connections?
EDIT:
As an example I'd like to do the following. Being on server A and knowing that server B is SSH-connected to me, I'd like to connect to connect to server B and place a command like
echo "This is test content." > /home/myuser/mytestfile`
After that I'd like to find a file
/home/myuser/mytestfile
in server B.
As @terdon said you can use the following command to execute something on the remote server through SSH and save (redirect) the output to the local instance:
Also you can use the following command to execute something on the local instance and pipe the output to the remote server and save it by the command
tee
:I'm agree with @Sebastian Stark you are probably asking how to create revers tunnel with SSH port forwarding. The SSH connection allows to bind port on the remote server to port on the local by using the option
-R
. And vice versa you can bind local port to remote port by-L
.In your case should be applied the first scenario by using the option
-R
:This command will establish connection from
HostA
toHostB
as usual and will bind port2222
onHostB
to port22
on the loopback interface onHostA
. That means when you request something on port2222
onHostB
the request will be handled by the service that listen on port22
onHostA
, usually this is the SSH server. At this point you could be able to use some of the following commands to connect back fromHostB
toHostA
:Or you can use the above commands, for example:
Note you should have installed SSH server on
HostA
!One interesting usage is that you can bind remote port on
HostB
to port on other instance in the local network ofHostA
:Where
192.168.100.115
is the IP address on any Windows computer in the LAN ofHostA
, let's call itHostC
. The options-fTN
will push the ssh connection into the background and you will have just a tunnel fromHostB:3389
throughHostA
toHostC:3389
.I'm using this by combination with
autossh
to keep the connection alive. For example I have the next line in mycrontab
:Where
remote-server-with-public-ip
is a Host defined in my~/.ssh/config
file onHostA
:The ports
2223
,6900
,3389
onHostB
are not public and I can access them only through another SSH connection - for example fromHostD
that is somewhere on Internet. But to access8080
I'm using Apache with reverse proxy onHostB
:-) and the virtual host's configuration file looks as this:proxy
andproxy_http
are required.Further reading: