Applescript to shutdown computers over local network

Hey guys!


I'm looking for a way to automate shutdown of 2 macs over a local network from a remote mac. I'm currently doing this by screen sharing in and shutting down both macs manually.


I have this untested theory that I could use my very basic knowledge of apple script to create a shutdown script on each mac and use an alias on the remote mac to run the script. However I am sure there is a much better way of doing this, I'm just lacking the know how! The script I want would ideally do as follows.


Run script from remote to Mac 1 and Mac 2 over network

Quit all running applications on Mac 1 and Mac 2

Shut down Mac 1 and Mac 2 in 5 minutes


If any one could help me imporve my laziness my giving me some pointers on this or even letting me know if its possible it would greatly appreciated!


Thanks!


C

Posted on Feb 12, 2014 4:33 PM

Reply
14 replies

Feb 14, 2014 1:46 PM in response to cibj

Introduction:


I have tried a few things over my local network. My attempt to run a remote AppleScript nearly shutdown the Mac I was activating it from. There are probably ways around this but I decided to try something else.


The following is a very stable solution once you have got it working. Each Mac to be shut down watches a folder called shut-down and if its contents change the Mac shuts down in one minute.


I am sure this can be improved upon.


Let me know how you get on.


Permissions:


It is simple to do all this as root user as described below but without 'with administrator privileges' at the end of the AppleScript.


If doing it as admin use the AppleScript as below. The plist may need permissions set to root wheel.


The shut-down folder will need appropriate sharing permissions.


Create plist etc:


open TextEdit

create a new document

set TextEdit format to plain text

copy plist contents below

paste into the TextEdit document

save as shut-down.plist

put document in folder /Library/LaunchAgents on each Mac to be shut down


create folders entitled shut-down at the root of Macs to be shut down


run launch AppleScript below on each Mac to be shut down


Future changes to the shut-down folder's contents will shut down the Mac.


Contents of shut-down.plist:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"

"http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>shut-down</string>

<key>ProgramArguments</key>

<array>

<string>shutdown</string>

<string>-h</string>

<string>+1</string>

</array>

<key>WatchPaths</key>

<array>

<string>/shut-down</string>

</array>

</dict>

</plist>


Launch AppleScript:


Put the following on one line;


do shell script "launchctl unload -w /Library/LaunchAgents/shut-down.plist; launchctl load -w /Library/LaunchAgents/shut-down.plist" with administrator privileges

Feb 17, 2014 5:00 PM in response to cibj

Here is a very simple way to shutdown a remote OS X Mac:


Terminal Command:

expect -c 'spawn ssh -o StrictHostKeyChecking=no remote-user@remote-IP; expect assword; send remote-password\r; expect remote-user$; send "sudo shutdown -h +1\r"; expect assword; send remote-password\r; interact'


Replace:


  1. remote-user with short login name of remote user
  2. remote-IP with the remote IP
  3. remote-password with the remote user's password


Notes:


  1. It works on OS X 10.5.8 and probably on most versions of OS X
  2. Put the Terminal command on one line
  3. Do not replace the IP with its name - occasionally fails
  4. Use double quotes around anything containing spaces
  5. The command assumes the remote prompt is 'remote-user$'
  6. Adjust as required or remove 'expect remote-user$;'
  7. Change '+1' to 'now' for instant shutdown or '+2' for 2 minutes etc


Use:


  1. Open a new TextEdit document and set its format to Plain Text
  2. Paste the above command into the document and edit as required
  3. Save as eg Shut-Mac-A.command
  4. Paste the following into Terminal: chmod u+x
  5. Type space in Terminal
  6. Drag the Shut-Mac-A.command file to Terminal
  7. Hit return key so that Terminal makes the file executable
  8. Put the file in a safe place
  9. Drag it to the right side of the Dock
  10. Run it by clicking as normal
  11. Do not be tempted to hide the Terminal window - you need to see if it fails
  12. There are more comprehensive and more secure ways of doing this




Feb 18, 2014 6:17 AM in response to Neville Hillyer

Re: Here is a very simple way to shutdown a remote OS X Mac


I forgot to mention that each remote Mac will need to have Remote Login enabled in Sharing Preferences - only allow access to those necessary.


This simple method is only intended for use over a firewall protected LAN - preferably not via wireless.


Please see the following for further details especially security concerns with passwords used via Expect.


SSH and Expect security:


  1. When to use Expect scripting and when to avoid it
  2. Automate SSH without public key authentication or Expect
  3. On SRP - some implementation notes and a critical review
  4. Top 20 OpenSSH server best security practices
  5. Secure remote logins and file copying

Feb 18, 2014 6:33 AM in response to cibj

This will work with three important limitations


  1. You need to enable Applescript Remote Events in the Sharing preferences on the remote machines
  2. Someone (not necessarily you) needs to be logged in on the remote machine.
  3. The shutdown is immediate, just as if you pressed Shutdown in the Apple menu on the remote machine.

set remoteMachine to "eppc://user:password@remotemachine.local"


using terms from application "Finder"

tell application "Finder" of machine remoteMachine

open application file id "com.apple.SystemEvents"

using terms from application "System Events"

tell application "System Events" of machine remoteMachine

shut down

end tell


end using terms from


end tell

end using terms from

And of course the password is in the script in clear text, could be a security problem.

Feb 18, 2014 1:24 PM in response to Frank Caggiano

That is interesting Frank.


I have not played with Remote Apple Events before.


On my Leopard I selected: 'Remote Apple Events' as I don't have: 'Applescript Remote Events'.


I did a few experiments and found that the following worked reliably.


tell application "Finder" of machine "eppc://remote-user:remote-password@remote-computer-name.local" to shut down


I am aware that security of Remote Apple Events was increased for Snow Leopard and am not sure if it will stop my simple AppleScript working. Perhaps somebody could try it.


Interesting observation:


During my tests of these AppleScripts and my earlier Expect scripts I was aware that too many incorrect remote accesses could result in things not working properly for some while without much warning. Sometimes an extra password or name / password pair were requested. This 'lockout' appeared similar to login attempts with the wrong name but with little or no visual indication and a much longer lockout period. Logging out and in did not reset it but I did not try a reboot. Is this documented anywhere?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to shutdown computers over local network

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.