How can I make a persistent ssh tunnel?

Posted by Blacklight Shining on Super User See other posts from Super User or by Blacklight Shining
Published on 2012-11-09T22:00:46Z Indexed on 2012/11/09 23:06 UTC
Read the original article Hit count: 241

Filed under:
|
|

I have a Mac laptop and would like to have a persistent ssh tunnel so I can always log in from outside the local network. I'm looking for something that will work when the server can't be reached initially (e.g. if I don't have an Internet connection when I boot it), and will automatically start the tunnel when possible.

I've tried putting an @reboot autossh line in my crontab, but I've found that sessions started with autossh disconnect every so often, and autossh quits if the first attempt fails. My current workaround is a small script and a cronjob:

# crontab
/home/blackl/bin/script &!

# script
#!/bin/sh
while true; do ssh -Ngn -R $some_port:localhost:22 $server; sleep 30; done;

Is there a better way to do this, or will I just have to be happy with this for now?

© Super User or respective owner

Related posts about osx

Related posts about ssh