5

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?

1 Answer 1

3

Sorry this is a very late response, and you may have come across this solution already, but here is how it can be done using OS X's launchctl mechanism.

I don't think he's got it quite right; you would use the "KeepAlive" key (with a value of "true") instead of the deprecated "OnDemand/false" key/value pair. Also, I don't know why he's forwarding to the identical port on the remote host (1666) as vs 22. Still - you get the idea. I've got mine up & running ok to our web server. Much more elegant than an infinite loop.

Oh, and instead of Lingon that he mentions, you can use the marvelous LaunchControl to set it up, modify and monitor it. If you're not familiar with launchctl, I strongly suggest reading the primer in the program or the site to understand what's going on. Sorry - I would have linked to the primer, but I haven't built up enough of a rep :/

4
  • 1
    Welcome to Super User! Please paraphrase the contents of the link in case the link disappears in the future.
    – oldmud0
    Commented Aug 4, 2016 at 16:38
  • Ok - to paraphrase, you have to set up a launchd.plist file which implements a command of the form: /usr/bin/ssh -CN -c blowfish -o 5 -L 1666:localhost:1666 <server> where <server> is defined in ~/.ssh/config. Use the KeepAlive/true key/value pair. Here, you're forwarding localhost:1666 to the remote <server>. man launchd.plist for how to do this - I haven't got enough characters left. :)
    – GAM
    Commented Aug 6, 2016 at 1:13
  • Thanks especially for the pointer to LaunchControl; it's a really slick tool.
    – dnault
    Commented Sep 12, 2016 at 18:46
  • Why not put it in the answer instead of comments!
    – AbiusX
    Commented Feb 23, 2021 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .