Jul 21 2008
Using AppleScript and a Shell Script to Restart an AppleTV remotely

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 > ~/sudoersthe 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.