2

I am looking for an easy way to automate my ssh tunnels.

Basically I want a local daemon listening a series of local ports. If one program sends traffic to that port, the daemon should start a SSH tunnel and forward that tunnel. At the same time, that tunnel should expire after certain time of no activity and close the connection until next time a request is made.

So far most solutions I have seen try to keep the tunnel open. However I just want the tunnel to exist automagically only when it is needed.

1 Answer 1

0

You can use a launchctl script and add it to /System/Library/LaunchAemons/smtp.plist. I'm currently using this to automatically set up a ssh tunnel for sending emails. The script then looks e.g.:

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST    1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
     <dict>
        <key>Label</key>
      <string>HOST.smtp</string>
    <key>ProgramArguments</key>
      <array>
        <string>/usr/bin/ssh</string>
        <string>-q</string>
        <string>-T</string>
        <string>-i</string>
        <string>/private/var/root/.ssh/smtpssh</string>
        <string>USER@EMAILHOST</string>
      </array>
    <key>Sockets</key>
      <dict>
        <key>Listeners</key>
          <dict>
            <key>Bonjour</key>
               <false/>
            <key>SockServiceName</key>
               <integer>PORT</integer>
            <key>SockType</key>
               <string>stream</string>
          </dict>
      </dict>
    <key>inetdCompatibility</key>
      <dict>
        <key>Wait</key>
          <false/>
      </dict>
  </dict>
</plist>

Of course you have to use public key authentication for that to work. In the .ssh/authorized_keys file you have to define a command like command="netcat smtp 25",no-X11-forwarding,no-agent-forwarding,no-port-forwarding <public key> in the line of the corresponding key.

You must log in to answer this question.

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