Filip Novotný
Filip Novotný

Reputation: 63

Remotely rebooting multiple Macs at once

We have a network of 9 Macs which I'd like to shut down all at once. Right now I'm using this AppleScript but as you can see, it uses SSH to shut down the computers one by one. And if one of them is already down, the script will freeze. Is there any way for me to shut all those Macs at once?

set finalIP to 100
repeat until finalIP is 109
tell application "Terminal"
    activate
    set success to 0
    do script "echo Rebooting.."
    do script "ssh -l username 192.168.1." & finalIP in front window
    delay 1
    repeat until success = 1
        if last word of (contents of front window as text) = "no" then
            do script "yes" in front window
            delay 1
        else if last word of (contents of front window as text) = "password" then
            do script "password" in front window
            set success to 1
            do script "echo About to reboot.." in front window
        else
            delay 1
        end if
    end repeat
    do script "sudo reboot" in front window
    repeat until last word of (contents of front window as text) = "password"
        delay 1
    end repeat
    do script "password" in front window
    close front window
end tell
set finalIP to finalIP + 1
delay 1
end repeat
tell application "Terminal" to quit

Upvotes: 1

Views: 156

Answers (2)

v1Axvw
v1Axvw

Reputation: 3054

The -o flag lets you specify a time out, so that when it doesn't succeed in connecting after the specified amount of seconds, it aborts.

imac$ ssh -o ConnectTimeout=5 123.45.6.7
ssh: connect to host 123.45.6.7 port 22: Operation timed out

Upvotes: 0

Mat
Mat

Reputation: 206689

You could simply set a timeout to the ssh connect:

ssh -o ConnectTimeout=10 -l ...

(This is a 10 second timeout, adjust to fit your need.)

Also really think about using keys for your connections. There is really no good reason to put SSH passwords in a script. (And if you do that, add the -o BatchMode=yes option too - it will skip hosts you can't log into with your key.)

Upvotes: 1

Related Questions