首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找关键字的命令输出

查找关键字的命令输出
EN

Stack Overflow用户
提问于 2015-04-29 09:40:16
回答 1查看 80关注 0票数 0

我想检查一下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请求到达,这就是为什么我尝试这样做的原因:

代码语言:javascript
运行
复制
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我把我的问题解决了如下:

代码语言:javascript
运行
复制
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。但是怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2015-04-29 09:48:18

ping以及任何其他方便的实用程序都会返回命令执行的状态。您不应该解析输出。

代码语言:javascript
运行
复制
system('ping -c 2 localhost')
#⇒ true
system('ping -c 2 unknown')
#⇒ false

您所需要的一切都是检查调用system的返回值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29939875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档