Estoy tratando de ejecutar un script al inicio para hacer un túnel a un host remoto sobre un puerto aleatorio.
Quiero dejar una cookie en la máquina remota para decir qué puerto he elegido.
Algo así:
# to make a record of the tunneling port on the remote host
echo $remoteport > $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 "user pid ppid pcpu pmem comm args" | egrep -v grep | egrep "${remoteport}:localhost"; do
echo found
sleep 30
done
echo finished at $(date)
Luego lo señalo desde un plist para que se ejecute al inicio.
Tengo varios problemas aquí.
- ¿Cómo ejecutar ssh en el inicio de sesión?
- ¿Cómo decirle a ssh que configure un puerto y luego mantenerlo abierto para siempre?
- ¿Cómo comprobar la conexión ssh para asegurarse de que sigue funcionando y restablecerla si se cae?
Sé que mi respuesta es a través de launchctl y ssh pero no consigo que todas las piezas se alineen.
FWIW, aquí está mi LaunchDaemon. No tengo ni idea de si funciona, ya que no puedo resolver los problemas de ssh primero. Esto es complicado.
root@Richards-iMac (~ ): cat /Library/LaunchDaemons/Tunneler.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/sbin</string>
</dict>
<key>GroupName</key>
<string>admin</string>
<key>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
<key>SuccessfulExit</key>
<true/>
</dict>
<key>Label</key>
<string>Tunneler</string>
<key>ProgramArguments</key>
<array>
<string>/Users/rcook/bin/linode_tunneler.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>rcook</string>
</dict>
</plist>