Reverse Screen Sharing Grabber

Dec 03, '10 07:30:00AM

Contributed by: aechadwick

I often need to (briefly) share my desktop with a room full of people, for example: to review drawings before printing. Since I dislike people standing behind me and staring over my shoulder, I wanted a way share my screen to a separate, public screen.

Ideally, the boss could say 'Put it on the big screen' and with one click I could be sharing -- all without anyone coming near my computer.

I work on a MacBook Pro, which is named 'W89100AK7AP.' In this case, I already had a big screen: the monitor attached to my Mac mini (named 'Dr-Cube' below) that spends the rest of the day as a jukebox.

Remotely starting Screen Sharing is easy. I saved the following line as GetScreenRemote.command and made it executable with chmod 755.

ssh Dr-Cube.local "osascript -e 'tell application "Screen Sharing" to open location "vnc://username:password@W89100AK7AP.local"'"
Dr-Cube (the mini) will be catching the screen from W89100AK7AP (the MacBook Pro with 'Remote Management' enabled). You'll need to change these to match the Bonjour names (or IP addresses) of your machines. The username:password does not have to be hardcoded into the script, I just wanted to make sure I was doing all the syntax correctly.

When run, the mini promptly grabs the screen of my computer and displays it, scaled to fit its own display. Sadly, there is no fullscreen Screen Sharing in Snow Leopard.

So far so good, but I want it run without asking for a password in terminal, so it's time to create an RSA Authentication Key between my computer and Dr Cube.

Note: I'm not really concerned about the security—even if someone actually wanted to control Dr-Cube, they'd only get access to my iTunes Library, there's nothing sensitive in it; as for my MacBook, it doesn't leave my sight.

Now, in the Terminal, type (pressing Enter after each of the commands to execute it):

ssh-keygen

which results in (you'll need to enter a name and location to save the resulting key file):
> Generating public/private rsa key pair.
> Enter file in which to save the key (~/.ssh/id_rsa): (enter)
> Enter passphrase (empty for no passphrase): (enter)
> Enter same passphrase again: (enter)
> Your identification has been saved in ~/.ssh/id_rsa.
> Your public key has been saved in ~/.ssh/id_rsa.pub.
> The key fingerprint is: (fingerprint)
> The key's randomart image is: (randomart)
then type:

mkdir /Volumes/dr-cube/.ssh

finally:

cp ~/.ssh/id_rsa.pub /Volumes/dr-cube/.ssh/authorized_keys

Again make sure to substitute the name of your Mac for dr-cube. Now the command runs without asking for a password.

One last step: I want it to run without even opening a terminal window. So I dropped the 'GetScreenRemote.command' into a standard location, and made a small AppleScript.

Enter the following into the AppleScript Editor:

do shell script "/Library/Scripts/GetScreenRemote.command"

Save As ScreenSender.app with File Format: Application, and we're done.

Now, with one click I can tell the mini to grab and display my screen!

Note: This is a single afternoon's worth of solution, and is not perfect. Ideally, the GetScreenRemote.command would switch itself on or off with each run, maybe using something based on this pseudocode:
if "ps ax | grep Screen Sharing | grep -v grep | awk '{print $1}'")  is "[no process]" then
ssh Dr-Cube.local "osascript -e 'tell application "Screen Sharing" to open location "vnc://username:password@W89100AK7AP.local"'"
else "kill -9 Screen Sharing"


[crarko adds: I tested most of this, and it works as described. If something breaks make sure you have changed the machine names to correctly match yours. Also note that leaving username:password as is will cause Screen Sharing to put up the login prompt, so you really don't have to hard code these in the script.]

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101127214443578