ssh shell script locks up

I have a powerbook that is constantly on insecure wireless networks, after a few minutes of using ethereal I realize I need a safe solution to check my POP mail.

My ultimate goal is to write a script which will replace the “Mail.app” in my dock.
The script will open a secure ssh tunnel to my mail server, than launch mail. This will allow my mail to be sent securly through a tunnell to me and keep prying eyes away.

So far:
I have my mail server set to use localhost:1110 as the POP server, I have forwarded my POP traffic to my mail server, and I have generated a pub and private key to bypass the SSH auth.

My AP script looks like this:
do shell script “/Users/chris/securemail.sh”

The shell script I am calling is this:
ssh -L 1110:MYSERVER.com:110 -l USER -N MYSERVER.com

When I execute this script from the command line I am successful, this opens my SSH tunnel and I can receive emails, when I execute the AP script, I am successful, can complete a POP, but the app just hangs. As soon as I launch it locks up, and I have to force quit. I am new to AP script but realize I need some way to terminate the tunnel.

Anyone have any ideas ?

I plan to write a long tutorial once I have this complete if anyone is interested, will be at macinstyle.com

Thanks !!

Actually, your app doesn’t lock up.

When you use ‘do shell script’, AppleScript waits for the shell command to return before continuing. So what’s happening is your script app is waiting for the ssh command to finish, which it won’t under normal circumstances.

Your best solution is to use a two-script approach. One is a modification to your script that opens the SSH tunnel and quits (or goes idle) leaving the tunnel active. The second script closes the connection.

To return control back to your script leaving the tunnel open, you need to append some arguments to the do shell script command:

do shell script "/Users/chris/securemail.sh > /dev/null 2>&1 &"{/code]

The mess on the end basically suppresses all output from the command and sends the process to the background. At this point your script will continue (or exit if there's no more code).

The second part of the process is to close the connection which is a little tricker.
One approach is the 'brute force' technique:

[code]do shell script "killall ssh"[/code]
which will terminate all ssh processes. If you expect the tunnel to be the only one then you might be OK, otherwise you run the risk of killing valid ssh connections.

To kill just the tunnel you need some way of identifying the process ID number, then just killing that process. If you need that, re-post and I'll see what I can come up with.

On the assumption that killing all ssh processes is OK, this should get you going:


on run
– open the ssh tunnel
do shell script “/Users/chris/securemail.sh >/dev/null 2>&1 &”
end run

on idle
– no need to do anything here, just return some value to keep the app alive
return 60
end idle

on quit
– user quit the script, so close all ssh connections
do shell script “killall ssh”
end quit



Save this script an a 'stay open' application and see how you go.

The script works great !!
the killall ssh would not be a problem, thanks so much for your help.

When I save the script as a stay open app I am unable to quit the app and have to force quit. Is there something I am missing ? I would like the ability to open the tunnel when I launch the app and close it when I close the app.

Any thoughts on that ?

Thanks again for the help.

You should be able to switch to it like any other application (click on its icon in the Dock, use Command-Tab, etc.) and then either use Command-Q or select Quit from the script application’s menu. If this doesn’t work, maybe modify the script to add a “return true” at the end of the “on quit” handler.

Jon

I think you should add a “continue quit” statement. Take a look to the concept “delegation”:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.f8.html#11377

(sorry, I’m the grind-faq-folk) :lol:

Opps, you got me, jj. I always forget that little gotcha. :slight_smile: