cerr cerr - 2 months ago 26
Linux Question

reverse ssh tunnel

I have an embedded device sitting out in the field, connected through a USB cell stick to the internet. In order to be able to reach it, I establish a reverse ssh tunnel on port 19996 to my home PC to be able to access it.

Now I re-establish that tunnel every 10min (by cron) to make sure, I have pretty much uninterrupted access even when the provider decides to change the my IP.

Now I realized that my process list is full of

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

and netstat is loaded up with connections as well. How can I ensure this doesn't happen? I only need omne tunnel at a time open and not 100s.

Answer Source

I wrote up followiung shell script which seems to work fine!

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