我们有一个由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发布于 2012-01-30 01:35:17
您可以简单地为ssh连接设置一个超时:
ssh -o ConnectTimeout=10 -l ...(这是10秒超时,请根据需要进行调整。)
另外,还可以考虑使用键进行连接。确实没有充分的理由将SSH密码放在脚本中。(如果您这样做了,还要添加-o BatchMode=yes选项-它将跳过您无法使用密钥登录的主机。)
发布于 2012-01-30 01:39:46
-o标志允许您指定一个超时时间,这样当它在指定的秒数之后没有成功连接时,它将中止。
imac$ ssh -o ConnectTimeout=5 123.45.6.7
ssh: connect to host 123.45.6.7 port 22: Operation timed outhttps://stackoverflow.com/questions/9055147
复制相似问题