一次远程重启多台Mac

时间:2012-01-29 17:28:27

标签: macos shell applescript

我们有一个由9台Mac组成的网络,我想立即关闭它们。 现在我正在使用AppleScript,但正如您所看到的,它使用SSH逐个关闭计算机。如果其中一个已经关闭,脚本将冻结。 有没有办法让我立刻关闭所有Mac?

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

2 个答案:

答案 0 :(得分:1)

您可以简单地为ssh connect设置超时:

ssh -o ConnectTimeout=10 -l ...

(这是10秒的超时,根据您的需要进行调整。)

还要考虑使用密钥进行连接。将SSH密码放在脚本中是没有充分理由的。 (如果你这样做,也可以添加-o BatchMode=yes选项 - 它会跳过你无法使用密钥登录的主机。)

答案 1 :(得分:0)

-o标志允许您指定超时,以便在指定的秒数后连接成功时,它会中止。

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