Remote Mac Control

I need to help out my grandmother with her mac occasionally. Now it’s much easier if I can just VNC straight into her machine. Unfortunately, it’s behind a router. But that’s OK. As usual, ssh tunnels to the rescue.

Unfortunately, she’s not really capable of using the command line. So it’s out with the AppleScript!

I wanted a little icon she could click to set up an ssh tunnel. That tunnel should then be torn down when the app closes. This is what I ended up with.

  global pid
  on run
    set sshCmd to "ssh -i ~/.ssh/vnc -R 5900:localhost:5900 -N"
    set pid to (do shell script sshCmd & "</dev/null >/dev/null 2>&1 & echo $!")
    display dialog "Connected to"
  end run
  on quit
    do shell script "kill " & pid
    continue quit
  end quit

There’s a few interesting points about this.

  • You have to redirect stdin/stdout/stderr to /dev/null or do shell script will never return.
  • I didn’t realise, but you capture the output of do shell script by enclosing it in parentheses. Yes, this is basic AppleScript. but it’s all new to me.
  • The on run and on quit handlers let you do Save As... application bundle. So you end up with a clickable application.
    • on quit must call continue quit or you end up with an immortal app…
  • The ssh command itself has a couple of interesting features.
    • -i ~/.ssh/vnc uses a custom key that I set up, instead of having to rely on a password.
    • -R 5900:localhost:5900 forwards port 5900 on back to port 5900 on her computer.
    • -N means “do nothing” instead of firing up some shell on my server.

At some point, I might extend this so that there’s a Window with a connect / disconnect button. But that will involve AppleScript Studio, which I don’t have on this computer.

So now I should be able to help clear up any, errr, “incidents”. Hopefully.

One reply on “Remote Mac Control”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s