AppleScript的为Ping测试SSH连接之前,每个客户端 [英] Applescript to ping test each client prior to ssh connection
问题描述
我试图让连接到一个列表本地SSH的机器,在一个新的终端窗口中的每个连接孔的AppleScript。在此之前尝试的ssh连接,我想ping客户端,看它是否可用:如果是这样,运行ssh命令,如果没有则迭代到下一个客户端。当我运行该脚本,它似乎在第一次连接工作,但随后给了我 - >错误号-10004
余下的客户端(和挂调试器)。任何反馈会大大AP preciated,谢谢!
设置主机{10.2.0.199,10.2.0.11,10.2.0.91,10.2.1.591,10.2.0.41}
设置的uname为{asus_client01,asrock_comp,msi003,gigabyte4,intel05client}
告诉应用程序终端
启用
与我重复1到主机的数
设置this_uname项我的uname --extract个人用户名
主机设置为字符串--extract的IPv4我的this_host项
设置uname_host为this_uname&安培; @&放大器;那么this_host
设置hostUp为真 尝试
做shell脚本的ping -c 1 -t 5&安培;那么this_host
出错
设置hostUp为false
显示对话框的this_host&安培; 似乎是下来。
延迟2
年底试 如果hostUp然后
做shell脚本SSH&放大器; uname_host
万一
重复结束
告诉结束
有做shell脚本
和做的剧本$之间的差异C $ C>。不同的是,
做shell脚本
是标准的脚本增加的一部分,将打开一个非交互的shell,执行给定的字符串,和stdout还给你回没有任何帮助从终端像其他应用程序。 做shell脚本
应该从未在其他任何用来告诉本身以外(我)应用程序块,因为你违反了一些AppleScript的证券可以在AppleScript的释放和技术说明找到。 做剧本
命令在应用终端的AppleScript命令的一部分。 做剧本
将进入目标窗口给定的字符串并执行就像你在自己的终端键入。 做剧本
仅受终端应用程序的支持,不能在其外部使用。
所以,要么做shell脚本:
做shell脚本平-o stackoverflow.com
或者通过使用终端做脚本
告诉应用程序终端
做剧本平-o stackoverflow.com
告诉结束
所以总的脚本可以是这个样子:
设置主机{10.2.0.199,10.2.0.11,10.2.0.91,10.2.1.591,10.2.0.41}
设置的uname为{asus_client01,asrock_comp,msi003,gigabyte4,intel05client}--security检查:主机列表中不能超过的uname长
如果(主机计数)> (UNAME计数),然后返回与我再说一遍,从1数主机
重复1次 - 模拟继续
设置currentAddress项我的主机
设置currentHostname项我的UNAME 如果不是((做shell脚本的ping -t -o 5&安培; currentAddress&安培;&放大器;>开发/空&放大器;&安培;回声是||回声无)作为布尔值),然后
退出重复 - 继续
万一 告诉应用程序终端
做剧本SSH&放大器; currentHostname
告诉结束
重复结束
重复结束
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
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.
So it's either do shell script:
do shell script "ping -o stackoverflow.com
or do script by using the Terminal
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的为Ping测试SSH连接之前,每个客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!