Skip to content

Screen Sharing via SSH With A Single Click

This tip uses AppleScript and shell script to create an AppleScript application that launches an ssh secured Screen Sharing (VNC client/server, introduced in OS X 10.5) to your remote machine.

First, make sure you have Screen Sharing enabled (System Preferences -> Sharing -> Screen Sharing).

Second, make sure you can use passwordless ssh login.

Third, create a shell script that contains the following:

ps axww | grep [s]sh | grep -q "[5]920:127.0.0.1:5900"
if [ $? != 0 ] ; then
  # ssh port forwarding not running, so start over
  ssh -N -f -L 5920:127.0.0.1:5900 username@remotehost
fi
open vnc://localhost:5920

replace username (e.g. johndoe), location (e.g. Library/Scripts) and remotehost (e.g. mymachine.homeip.net) with your details. Save this shell script to the location, giving it a .command suffix (e.g. ssh_and_share.command), and making it executable (e.g., run chmod 755 ssh_and_share.command in Terminal). (Static IP really helps things up. If your router doesn't support, say, DynDNS out of the box, you might want to try an alternative method.)

You can now launch the ssh_and_share.command, either from command line, or dragging the file to your Dock and executing it from there. However, because this is a shell script it uses Terminal and opens an extra Terminal window. We'll get rid of it.

Open Script Editor (in Applications -> AppleScript) and type in:

do shell script "/Users/username/location/ssh_and_share.command"

changing username and location, and save it in your location. Now drag this file to your Dock, and voilà! No more Terminal.

Kudos to Jay Ward for letting me know how to open a vnc connection in a shell script.

(If you know how to omit the terminal window without AppleScript, please let me know. Or, if you know how to perform the actions in ssh_and_share.command with AppleScript, again, please let me know.)

UPDATE: More ideas here.