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. If you’re using an older patchstick, the process is a little different, since it may only support 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 /etc/sudoers > ~/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 (chmod 0440) and ownership (chown 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'

(drop the -1 for newer Patchsticks)

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 &"

(drop the -1 for newer Patchsticks)

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.

Advertisements

4 thoughts on “Using AppleScript and a Shell Script to Restart an AppleTV remotely

  1. Hi,

    I try your trick but before move the sudoers file I screw out of sudo, no set owership…

    you have a clue to repare this mistake?

    thank

  2. i solved this in a simple script, no need to remove ssh password

    see appletv wiki forums

    http://forum.atvflash.com/viewtopic.php?f=8&t=3116

    script =

    property commandString : “ssh frontrow@appletv.local
    property commandString1 : “frontrow”
    property commandString2 : “sudo reboot”

    tell application “Terminal”
    launch
    set visible of window 1 to false
    if (count of windows) is 1 then
    do script commandString in window 1
    delay 1
    do script commandString1 in window 1
    delay 1
    do script commandString2 in window 1
    delay 1
    do script commandString1 in window 1
    else
    set frontmost of window 1 to true
    do script commandString in window 1
    delay 1
    do script commandString1 in window 1
    delay 1
    do script commandString2 in window 1
    delay 1
    do script commandString1 in window 1
    end if
    end tell
    delay 1
    tell application “Terminal”
    if (count of windows) is 1 then
    quit
    else
    set frontmost of window 1 to true
    end if
    end tell
    tell application “Finder”
    display dialog “ATV Rebooted Satis”
    end tell

    copy that straight into applescript then run. can save as app if required

Comments are closed.