Wednesday, March 21, 2012

Persistent SSH Tunnel

I always have a box reverse ssh to one of my other boxes. This tunnel needs to stay up at all times and it need to come back after restarts. This is the solution:

Firstly I need to know exactly what command I will use for the reverse connection. This is mine:
  • ssh -i /home/loldongs/.ssh/lin -N -T -R 4510:localhost:22
Great, now i need a "watcher" script that will constantly check if the connection is up; if so, do nothing; if not, start the damn thing.
#make sure a process is always running.

export DISPLAY=:0 #needed if you are running a simple gui app.

process='ssh -i /home/loldongs/.ssh/lin -N -T -R 4510:localhost:22'
makerun='ssh -i /home/loldongs/.ssh/lin -N -T -R 4510:localhost:22'

if ps ax | grep -v grep | grep "$process" > /dev/null
        $makerun &
I found this somewhere on the internet, i'm loving it. I mainly love it because all i have to do is call this script from cron every minute and i will ensure that the connection is never down for more than 60seconds.

No comments:

Post a Comment