Applescript 在 ssh 连接之前对每个客户端进行 ping 测试 [英] Applescript to ping test each client prior to ssh connection
问题描述
我正在尝试制作一个连接到本地 ssh 机器列表的 Applescript,每个连接都在一个新的终端窗口中打开.在尝试 ssh 连接之前,我想 ping 客户端以查看它是否可用:如果可用,则运行 ssh 命令,如果不可用,则迭代到下一个客户端.当我运行脚本时,它似乎适用于第一个连接,但随后给了我 -->其余客户端的错误号 -10004
(并挂起调试器).任何反馈将不胜感激,谢谢!
I'm trying to make an Applescript that connects to a list local ssh machines, with each connection opening in a new terminal window. Prior to attempting the ssh connection, I'd like to ping the client to see if it's available: if it is, run the ssh command, if not then iterates to the next client. When I run the script it seems to work for the first connection but then gives me --> error number -10004
for the remaining clients (and hangs the debugger). Any feedback would be greatly appreciated, thanks!
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 script
和 do script
是有区别的.不同之处在于 do shell script
是标准脚本添加的一部分,它将打开一个非交互式 shell,执行给定的字符串,然后将 stdout 返回给您,而无需来自其他应用程序(如终端)的任何帮助.do shell script
不应该在任何其他 tell 应用程序块中使用,除了它自己 (me),因为你违反了一些 AppleScript 安全,你可以在 AppleScript 版本和技术说明中找到.do script
命令是应用程序终端中 AppleScript 命令的一部分.do script
将在目标窗口中输入给定的字符串,并像您自己在终端中输入一样执行该字符串.do script
仅终端应用支持,不能在终端应用之外使用.
There is a difference between do shell script
and do script
. The difference is that do shell script
is part of the standard script addition and will open an non-interactive shell, execute the given string, and return stdout back to you without any help from another application like Terminal. do shell script
should never been used in any other tell application block except itself (me) because you violate some AppleScript securities you can find in AppleScript release and technical notes. do script
command is part of AppleScript command in the application Terminal. do script
will enter the given string in the targeted window and execute that like you have typed in Terminal yourself. do script
is only supported by Terminal application and can't be used outside of it.
所以要么做shell脚本:
So it's either do shell script:
do shell script "ping -o stackoverflow.com
或使用终端执行脚本
tell application "Terminal"
do script "ping -o stackoverflow.com"
end tell
所以整个脚本可能看起来像这样:
So the total script could look something like this:
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
这篇关于Applescript 在 ssh 连接之前对每个客户端进行 ping 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!