Reverse SSH tunnel

From Daniel's homepage

Jump to: navigation, search

I put the following script in a file which is called each minute from /etc/crontab

#! /bin/bash
procfile=/proc/`cat /var/run/ssh_remotecomputer.pid`/cmdline
ssh_hash=`cat $procfile|md5sum|cut -f1 -d' '`
if [ $ssh_hash != "318cf42eac73cd393349c88657b54af2" ]
then
  echo Starting ssh connection
  ssh -o TCPKeepAlive=yes -NR 10000:localhost:22 -i /root/.ssh/tunneluser_id_rsa tunneluser@remotecomputer.de & childpid=$!
  echo $childpid > /var/run/ssh_up2p77.pid
fi

The hash is used to verify that the process running under the pid really is the ssh we need for the tunnel, and not any other process which by chance (maybe after a reboot) got the same pid. You'll have to adjust it to match your commandline. Check the contents of $ssh_hash!

This setup makes sure that even after DSL disconnects or other quirks the connection always comes back up as soon as the connection is restored.

On the remotecomputer I can now call ssh root@localhost -p10000 to log onto the computer behind the DSL router.

Personal tools