我想检查一下ping是否成功。现在,我遇到了这样一个问题,即手动ping的结果是这样的:
麻省理工学院32 Bytes Daten Antwort von 10.128.129.15: Zielhost nicht erreichbar. Antwort von 10.128.129.15: Zielhost nicht erreichbar. Antwort von 10.128.129.15: Zielhost nicht erreichbar. Ping-Statistik für 10.128.129.15: 帕克特: Gesendet = 3,Empfangen = 3,Verloren =0 (0% Verlust),# which would be an "- Success" output in my script
对于每一个非德国人,ping命令都能工作,但最终它说DestinationHost是无法到达的。我想检查它是否可以通过ping请求到达,这就是为什么我尝试这样做的原因:
def pingtest(destination,device)
print(" Pinging #{destination} #{device}: ")
#I tried to get the system output with several methods, the ping is printed
#in the consol but in the end it just returns "true"
output = system("ping -n 3 #{destination} 2>&1") #later I want >nul
puts output #prints "true"
#I tried some stuff like this: the .include? method was an
# idea from ***another Question (with ssh) and this worked
if output.content.include?("Zielhost nicht erreichbar")
puts "Success Debug"
end
#Backtick to check if the ping succeded or not..
if $? == 0
puts " - Success\n"
return true
else
puts(" - Failed\n")
return false
end
end
*another question我把我的问题解决了如下:
result = ssh.exec!"ping -c 3 #{@dest_ip}"
if result.include?("100% packet loss") || result.include?("Zeitüberschreitung") || result.include?("100% Verlust") || and so on
我现在还没有为我的问题找到一个工作解决方案(没有SSH连接),所以也许您会有一个解决方案。我的目标是安全地说一个设备是否可以在网络中访问。也许对我的项目有一个简短的回顾:我有两个NAT网关与RasberyPI一起构建--有两个profinet和一个网络,包括这两个profinet。现在我要检查这两个NAT网关网络中的每个连接。在本例中,我是一个运行此脚本的Service_PC。在运行ping时,或多或少我需要检查命令promt。但是怎么做呢?
发布于 2015-04-29 09:48:18
ping
以及任何其他方便的实用程序都会返回命令执行的状态。您不应该解析输出。
system('ping -c 2 localhost')
#⇒ true
system('ping -c 2 unknown')
#⇒ false
您所需要的一切都是检查调用system
的返回值。
https://stackoverflow.com/questions/29939875
复制相似问题