How to get an ssh tunnel started every time my Mac boots? I'm trying to run a script at startup to tunnel to a remote host over a random port.
I want to leave a cookie on the remote machine to say which port I chose.
Something like this:
# to make a record of the tunneling port on the remote host
echo $remoteport &gt; $portfile
scp $portfile rcook@linode.richcook.net:  
# to actually do the tunnel and go into the background
ssh -n -i /Users/rcook/.ssh/id_rsa -o ExitOnForwardFailure=yes -R $remoteport:localhost:22 rcook@linode.richcook.net 
# to make a record of the tunneling port on the remote host
# keep checking if ssh is up
while ps -Awwx -o &quot;user pid ppid pcpu pmem comm args&quot; |  egrep -v grep |  egrep &quot;${remoteport}:localhost&quot;; do
       echo found
       sleep 30
done
echo finished at $(date)

I then point to it from a plist to run at startup.
I'm having multiple problems here.

How to run ssh at login?
How to tell ssh to set up a port and then just hold the port open forever?
How to check on the ssh connection to make sure it's still up and reestablish if it goes down?

I know my answer is through launchctl and ssh but I just can't get all the pieces to line up.
FWIW, here is my LaunchDaemon.  Have no idea if it works, since I can't solve the ssh problems first.  This is complicated.
root@Richards-iMac (~ ): cat /Library/LaunchDaemons/Tunneler.plist 
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
&lt;plist version=&quot;1.0&quot;&gt;
&lt;dict&gt;
    &lt;key&gt;EnvironmentVariables&lt;/key&gt;
    &lt;dict&gt;
        &lt;key&gt;PATH&lt;/key&gt;
        &lt;string&gt;/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/sbin&lt;/string&gt;
    &lt;/dict&gt;
    &lt;key&gt;GroupName&lt;/key&gt;
    &lt;string&gt;admin&lt;/string&gt;
    &lt;key&gt;KeepAlive&lt;/key&gt;
    &lt;dict&gt;
        &lt;key&gt;Crashed&lt;/key&gt;
        &lt;true/&gt;
        &lt;key&gt;SuccessfulExit&lt;/key&gt;
        &lt;true/&gt;
    &lt;/dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;Tunneler&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Users/rcook/bin/linode_tunneler.sh&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;UserName&lt;/key&gt;
    &lt;string&gt;rcook&lt;/string&gt;
&lt;/dict&gt;
&lt;/plist&gt;
OK, no sooner asked than I figured out the last few pieces.
To get ssh to do what I wanted, here is the shell script to do what I want:
#!/usr/bin/env bash

echo $0 run on $(date)
# Just chose an arbitrary 401 value range of random numbers.  
randport=$(/usr/bin/python -S -c &quot;import random; print random.randrange(5555,5955)&quot;)
remoteport=$randport # this way we always know what port we are using.  Fix other problems later... 

portfile=/Users/rcook/.ssh/linode_remote_port.txt

echo $remoteport &gt; $portfile
scp $portfile rcook@linode.richcook.net:
ssh -N -n -i /Users/rcook/.ssh/id_rsa -o ExitOnForwardFailure=yes -R $remoteport:localhost:22 rcook@linode.richcook.net &amp;

sleep 5
while ps -Awwx -o &quot;user pid ppid pcpu pmem comm args&quot; |  egrep -v grep |  egrep &quot;${remoteport}:localhost&quot;; do
       echo found
       sleep 30
done
echo finished at $(date)

I put the launchctl script into /Library/LaunchDaemons/Tunneler.plist.
This way it loads at system launch.