Jul 21 2008

Using AppleScript and a Shell Script to Restart an AppleTV remotely

Published by Jonathan Wise under Hacks

Both my media serving devices (a NAS, and a Mac Mini) are using TwonkyVision Media Server to share media using uPnP. It works great, but as I mentioned earlier, I needed a script to make it start back up in the morning.

Unfortunately, I’ve found that my AppleTV freaks out a bit when this happens, and as a result, needs its own reboot. Restarting the Finder helps, but streaming still gets weird, so a full, daily reboot is in order. Using the same iCal-alarm-firing-an-AppleScript trick, I updated my Twonky restart script to also tell the AppleTV to restart. But it wasn’t that easy.

  • First of all, you need to exchange keypairs with the AppleTV so that you can login over ssh without a password. However, the process is a little different, since the AppleTV only supports ssh1. This wiki page explains the process, and the slight tweak to it for ssh1.
  • Second, you need to set the AppleTV up to allow you to sudo without a password. This is harder than it sounds, and requires modifying the /etc/sudoers file on the AppleTV. The only way I found to do that was to sudo cat > ~/sudoers the file (which dumps the contents of one file to another, which you can access more easily), copy it over to my Mac, edit it, copy it back, set the permissions, and sudo mv it back over-top the original. You’ll need to add this line to the bottom of the sudoers file:
    frontrow ALL=(ALL) NOPASSWD: ALL
  • Make sure you change the permissions (0440) and ownership (root:wheel) on your new sudoers before you replace the original, or you’ll screw yourself out of sudo!

To test those things, you could go to Terminal on your Mac and try something like:
ssh -1 frontrow@appletv.local 'sudo ls-l'
If you’ve setup everything right, you shouldn’t get prompted for a password to login OR to get a directory listing. Once that works, the AppleScript is easy and looks like this:

do shell script "ssh -1 frontrow@appletv.local 'sudo reboot' &> /dev/null &"

At some point, I’m also going to attach this to a PHP script (somehow) so I can use Safari on my iPhone to do a reboot from the couch if needed. I’ve only found two other ways to reboot the AppleTV — yanking the power cord, or using the remote to put it in recovery mode. Neither seems as elegant as my solution.

No responses yet

Sep 21 2007

Generating RSA Keypairs on Mac OS X for a *nix web-server

Published by Jonathan Wise under Hacks

I’m sure there are many of you who have long been comfortable with RSA keypairs, so I post this as much for my own reference, as for those like me who stumble around the Internet trying to find coherent instructions on how to do this. These instructions work for OS X 10.4 — not sure about other versions or *nixes. I plan to apply them to securing SSH on my iPhone for easier file transfers though.

Step 1: Open Terminal
Usually found in the Utilities folder of Applications (but if you didn’t know that already, this might not be the right topic for you!)

Step 2: Create your public/private key pair on your local computer

  • At the command prompt type: ssh-keygen -t rsa
  • Hit enter to accept the default file name and location

Step 3: Copy your public key contents into the “authorized_keys2″ file on the remote server

  • Type: ssh username@remoteserver.com ‘cat >> ~/.ssh/authorized_keys2′ < /Users/Youruser/.ssh/id_rsa.pub
  • Enter your FTP password for the remote server when prompted
    • If the file didn’t already exist on the remote server, it will be created, don’t worry.

Step 4: SSH at will!
You will never need a password from your account on your local computer again! Note that you’ll need a public key provided for each client computer/computer account you want to use to connect to the server.

One response so far