gpt4 book ai didi

macos - AppleScript在SSH连接之前ping测试每个客户端

转载 作者:行者123 更新时间:2023-12-02 14:09:43 34 4
gpt4 key购买 nike

我试图使一个Applescript连接到列表本地ssh机器,每个连接在新的终端窗口中打开。在尝试ssh连接之前,我想对客户端进行ping操作以查看其是否可用:如果可用,请运行ssh命令,如果没有,则迭代到下一个客户端。当我运行脚本时,它似乎对第一个连接有效,但随后为其余客户端提供了--> error number -10004(并挂起了调试器)。任何反馈将不胜感激,谢谢!

set hosts to {"10.2.0.199", "10.2.0.11", "10.2.0.91", "10.2.1.591", "10.2.0.41"}
set uname to {"asus_client01", "asrock_comp", "msi003", "gigabyte4", "intel05client"}
tell application "Terminal"
activate
repeat with i from 1 to the count of hosts
set this_uname to item i of uname --extract individual username
set this_host to item i of hosts as string --extract iPv4
set uname_host to this_uname & "@" & this_host
set hostUp to true

try
do shell script "ping -c 1 -t 5 " & this_host
on error
set hostUp to false
display dialog this_host & " seems to be down."
delay 2
end try

if hostUp then
do shell script "ssh " & uname_host
end if
end repeat
end tell

最佳答案

do shell scriptdo script之间有区别。区别在于do shell script是标准脚本添加的一部分,它将打开一个非交互式shell,执行给定的字符串,并在没有其他应用程序(如Terminal)的帮助下将stdout返回给您。 do shell script切勿在自身(我)以外的任何其他Tell应用程序块中使用,因为您违反了AppleScript发行版和技术说明中可以找到的某些AppleScript证券。 do script命令是应用程序终端中AppleScript命令的一部分。 do script将在目标窗口中输入给定的字符串,并像您自己在Terminal中键入的那样执行。 do script仅受终端应用程序支持,不能在其外部使用。

所以要么做shell脚本:

do shell script "ping -o stackoverflow.com

或使用终端做脚本
tell application "Terminal"
do script "ping -o stackoverflow.com"
end tell

因此,总脚本可能如下所示:
set hosts to {"10.2.0.199", "10.2.0.11", "10.2.0.91", "10.2.1.591", "10.2.0.41"}
set uname to {"asus_client01", "asrock_comp", "msi003", "gigabyte4", "intel05client"}

--security check: hosts list can't be longer than uname
if (count of hosts) > (count of uname) then return

repeat with i from 1 to count hosts
repeat 1 times -- simulate continue
set currentAddress to item i of hosts
set currentHostname to item i of uname

if not ((do shell script "ping -o -t 5 " & currentAddress & "&>dev/null && echo yes || echo no") as boolean) then
exit repeat -- continue
end if

tell application "Terminal"
do script "ssh " & currentHostname
end tell
end repeat
end repeat

关于macos - AppleScript在SSH连接之前ping测试每个客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22649895/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com