【问题标题】:Remotely rebooting multiple Macs at once一次远程重启多台 Mac
【发布时间】:2012-01-29 17:28:27
【问题描述】:

我们有一个由 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

【问题讨论】:

    标签: macos shell applescript


    【解决方案1】:

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

    ssh -o ConnectTimeout=10 -l ...
    

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

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

    【讨论】:

      【解决方案2】:

      -o 标志可让您指定超时时间,以便在指定秒数后连接失败时中止。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-08
        • 1970-01-01
        • 1970-01-01
        • 2017-02-04
        相关资源
        最近更新 更多