

$cmd = "ssh $localhost -p $iport -o UserKnownHostsFile=/dev/null -o StrictHostKe圜hecking=no" As a result, the first ssh command keeps the tunnel open until the following two conditions are satisfied: sleep 10 is finished and the tunnel is no longer used.Īfter reading the above and glueing everything together, I've created the following Perl script (save it as mssh in /usr/bin and make it executable): #!/usr/bin/perl Usually, the first ssh command would close after 10 seconds, but during this time, the second ssh command will have established a connection using the tunnel. After closing the private ssh session, I want the ssh tunnel to close, too.Only if this is successful, ssh into the private machine using the tunnel.Establish a tunnel for the ssh protocol (port 22) to the private machine.Ssh -f -L some_port:private_machine:22 "sleep 10" & ssh -p some_port is happening: To automate this procedure, I use the following script: #!/bin/bash If I'm outside and want a remote shell on a machine inside the private network, I would have to ssh into the gateway and from there to the private machine.

We have one ssh gateway into our private network. Option 3 is mainly useful to access a service on host2 that is only reachable from host2 itself. If the connection from host1 to host2 needs to be secured, go with option 2. Then a second tunnel is opened from localhost to host2 through the first tunnel. This will open a tunnel from localhost to host1 through which the SSH service on host2 can be used. Tunnel from localhost to host1 and from localhost to host2: ssh -L 9998:host2:22 -N host1 However the port 9999 to host2:1234 can be used by anyone on host1. This will open a tunnel from localhost to host1 and another tunnel from host1 to host2. Tunnel from localhost to host1 and from host1 to host2: ssh -L 9999:localhost:9999 host1 ssh -L 9999:localhost:1234 -N host2 Tunnel from localhost to host1: ssh -L 9999:host2:1234 -N host1Īs noted above, the connection from host1 to host2 will not be secured.
