Remote Shutdown Help

OK, i have a script that shutsdown a mac remotely,

However, i had to make a script for each mac i look after (40+)

Question 1;
Is is possible to make it so that i can just enter all the mac’s into one script to shutdown?

Question 2;
Is it possible to bring up an input box and enter the mac name you want to shut down?

heres my scripts so far;


tell application "Finder" to activate application "Terminal"
delay 0.5
tell application "System Events" to keystroke "ssh root@00001.local"
tell application "System Events" to keystroke return
delay 1
tell application "System Events" to keystroke "password"
tell application "System Events" to keystroke return
tell application "System Events" to keystroke "shutdown -h now"
tell application "System Events" to keystroke return

Also, if the mac im trying to shutdown is already off, it turns the mac im running the script on off, any ideas to get around that?

Ive got the script to ask you for the tag number now,


set TagAnswer to ""
display dialog "Enter Tag Number To Shutdown" default answer TagAnswer buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop
set TagAnswer to text returned of the result
tell application "Finder" to activate application "Terminal"
delay 0.5
tell application "System Events" to keystroke "ssh root@ " & TagAnswer & ".local"
tell application "System Events" to keystroke return
delay 1
tell application "System Events" to keystroke "password"
tell application "System Events" to keystroke return
tell application "System Events" to keystroke "shutdown -h now"
tell application "System Events" to keystroke return

Could i set “TagAnswer” to point to a txt file with a list of tag numbers to shut down?

Just realized you posted again, but how about something like this? (fyi I havent actually tested this yet, I’m not in the office yet)

The requirement of this though is that you need OpenSSH public/private keys set up for the nonauthentcated ssh logins.

set machineList to {¬
	{"User 1", "00001.local"}, ¬
	{"User 2", "00002.local"}, ¬
	{"User 3", "00003.local"}}

set userList to {}

repeat with i in machineList
	set end of userList to item 1 of i
end repeat

set shutdownList to choose from list userList with multiple selections allowed

repeat with i in shutdownList
	repeat with x in machineList
		if contents of i is equal to contents of (item 1 of x) then
			display dialog "shutting down " & (item 2 of x)
			do shell script "ssh root@" & (item 2 of x) & "; sleep 1; shutdown -h now"
		end if
	end repeat
end repeat

Or you could get ARD :wink:

I obviously can’t test this, [and I see that Jay beat me to it], but something like this might work for you:


property Head : "ssh root@ "
property Tail : ".local
password
shutdown -h now
"

set SD to button returned of (display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"})
if SD = "One Mac" then
	set Tag to text returned of (display dialog "Enter Tag Number To Shutdown" default answer "" buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop)
	doShutDown(Tag)
else if SD = "All Macs" then
	set AllTags to paragraphs of (read alias (path to desktop folder as text) & "TagList.txt") -- one per line
	repeat with aTag in AllTags
		doShutDown(contents of aTag)
	end repeat
else
	return
end if

to doShutDown(someTag)
	do shell script Head & aTag & Tail
end doShutDown

Note that it is not necessary to use the terminal; AppleScript supports ‘do shell script “theCommands”’.
TagList.txt is assumed to be a text file (not rtf) containing one tag per line.

I havn’t tried James’s idea yet as i already have the tag numbers in a txt file, however when i try your idea adam it fails on this line;

read alias (path to desktop folder as text) & "TagList.txt"

And i get the error “can not make alias”

i like the option to pick one or all btw :smiley:

your missing some brackets there =)

read alias ((path to desktop folder as text) & "TagList.txt")

My Mistake, missed them out in my reply but it still comes up with the error

does TagList.txt exist on your desktop? If it does can you post an updated version of the script there may be something else going on because I tested it and it works for me.

its just the same as adams post, im running it from root user atm, but have tried it under my normal admin user and still no luck.

EDIT:

Here is the error log;

tell current application
	display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"} default button "Cancel" with icon caution
		{button returned:"All Macs"}
	path to desktop as text
		"Hard disk:private:var:root:Desktop:"
	read {alias "Hard disk:private:var:root:Desktop:", "TagList.txt"}
		"Can't make {alias \"Hard disk:private:var:root:Desktop:\", \"TagList.txt\"} into type file."

This works for me:


property Head : "ssh root@ "
property Tail : ".local
password
shutdown -h now
"

set SD to button returned of (display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"})
if SD = "One Mac" then
	set Tag to text returned of (display dialog "Enter Tag Number To Shutdown" default answer "" buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop)
	doShutDown(Tag)
else if SD = "All Macs" then
	set AllTags to paragraphs of (read alias ((path to desktop folder as text) & "TagList.txt")) -- NOTE Parens.
	repeat with aTag in AllTags
		--doShutDown(contents of aTag)
		log aTag
	end repeat
else
	return
end if

to doShutDown(someTag)
	do shell script Head & aTag & Tail
end doShutDown

What you’re really setting up here is:

create a path → (path to desktop folder as text) & “TagList.txt”)
create file refererence → alias (a path) → alias ((path to desktop folder as text) & “TagList.txt”))
read the referenced file → read (alias a path) → read alias ((path to desktop folder as text) & “TagList.txt”)
get paragraphs of file text → paragraphs of (read alias ((path to desktop folder as text) & “TagList.txt”))

Im still having some trouble…


property Head : "ssh root@"
property Tail : ".local"
property pword : "password"
property shutdown : "shutdown -h now"

set SD to button returned of (display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"} default button "Cancel" with icon caution)
if SD = "One Mac" then
	set Tag to text returned of (display dialog "Enter Tag Number To Shutdown" default answer "" buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop)
	doShutDown(Tag)
else if SD = "All Macs" then
	set AllTags to paragraphs of (read alias ((path to desktop folder as text) & "TagList.txt")) -- one per line
	repeat with aTag in AllTags
		log aTag
	end repeat
else
	return
end if

to doShutDown(someTag)
	do shell script Head & aTag & Tail
	delay 1
	do shell script pword
	delay 1
	do shell script shutdown
end doShutDown

When i select all mac’s, it goes through but doesnt shut them down…
And when i select one mac it comes up with an error saying aTag is not defined…

Heres the event log for each:

One Mac;


tell current application
	display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"} default button "Cancel" with icon caution
		{button returned:"One Mac"}
	display dialog "Enter Tag Number To Shutdown" default answer "" buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop
		{text returned:"00001", button returned:"Shutdown"}
		"The variable aTag is not defined."


All Mac’s;


tell current application
	display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"} default button "Cancel" with icon caution
		{button returned:"All Macs"}
	path to desktop as text
		"Hard disk:private:var:root:Desktop:"
	read alias "Hard disk:private:var:root:Desktop:TagList.txt"
		"{\\rtf1\\mac\\ansicpg10000\\cocoartf824\\cocoasubrtf410
{\\fonttbl\\f0\\fswiss\\fcharset77 Helvetica;}
{\\colortbl;\\red255\\green255\\blue255;}
\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww9000\\viewh8400\\viewkind0
\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\\tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural

\\f0\\fs24 \\cf0 13152\\
13153}"
	(*{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf410*)
	(*{\fonttbl\f0\fswiss\fcharset77 Helvetica;}*)
	(*{\colortbl;\red255\green255\blue255;}*)
	(*\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0*)
	(*\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural*)
	(**)
	(*\f0\fs24 \cf0 13152\*)
	(*13153}*)
end tell

Give this a try, I spotted two things that needed fixing (well at least I think they did LOL)


property Head : "ssh root@"
property Tail : ".local"
property pword : "password"
property shutdown : "shutdown -h now"

set SD to button returned of (display dialog "Shut down one machine or all?" buttons {"Cancel", "One Mac", "All Macs"} default button "Cancel" with icon caution)
if SD = "One Mac" then
	set Tag to text returned of (display dialog "Enter Tag Number To Shutdown" default answer "" buttons {"Shutdown", "Cancel"} default button "Cancel" with icon stop)
	doShutDown(Tag)
else if SD = "All Macs" then
	set AllTags to paragraphs of (read alias ((path to desktop folder as text) & "TagList.txt")) -- one per line
	repeat with aTag in AllTags
		doShutDown(aTag)
	end repeat
else
	return
end if

to doShutDown(someTag)
	do shell script Head & someTag & Tail
	delay 1
	do shell script pword
	delay 1
	do shell script shutdown
end doShutDown

I get an error this time, lol;


"Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: {rtf1macansicpg10000cocoartf824cocoasubrtf410.local: No address associated with nodename"

This is the line is highlights;


do shell script Head & someTag & Tail

Ive had aplay myself and cant get either on or all mac’s working :confused:

Is “TagList.txt” a rich text file?

it was an .rtf but renamed it as a .txt

Renaming a file doesn’t change it’s contents. It looks like your script is choking on the RTF formatting information.

My bad!

durr!

Remade the txt file and still not working,

Have you connected to these machines with SSH before?

yes, if i use SSh in terminal it works fine, on any machine on the list

is that “{” suppossed to be in the tag name?