Linux – Reverse ssh tunneling

Reverse ssh tunneling… here is a solution to the problem.

Reverse ssh tunneling

I have an embedded device in the field that is connected to the internet via a USB battery stick. To be able to access it, I set up a reverse ssh tunnel on port 19996 to my home PC to be able to access it.

Now, I re-establish that tunnel every 10 minutes (via cron) to ensure that my access is almost uninterrupted even if the provider decides to change my IP.

Now I realize that my process list is full

"4383 root     ssh -R 19996:localhost:22 -f -N user@host"

And netstat also loads the connection. How can I make sure this doesn’t happen? I only need to open one tunnel at a time, not 100.

Solution

I wrote the following shell script that seems to work fine!

#!/bin/sh
RETVAL=`netstat | grep 'S0106b0487afe2a57'| grep -c 'ssh ESTABLISHED'`
echo "${RETVAL} open tunnel(s)"
if [ "$RETVAL" -lt "1" ]
  then
        echo "starting reverse ssh tunnel"
        `ssh -R 19999:localhost:22 -f -N user@host`
        echo "done"
fi

Related Problems and Solutions