1.9.1 的 ruby​​ ping [英] ruby ping for 1.9.1

查看:28
本文介绍了1.9.1 的 ruby​​ ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ruby​​ 代码中 ping 一个站点,并且看到 net-ping 是一个很好的库来执行此操作.不幸的是,当我尝试 gem install net-ping 时出现以下错误:

I want to ping a site in my ruby code and saw that net-ping was a nice library to do this with. Unfortunately, when I tried to gem install net-ping I got the following error:

C:>gem 安装 net-ping
错误:安装 net-ping 时出错:
win32-open3 需要 Ruby 版本 <1.9.0.

C:>gem install net-ping
ERROR: Error installing net-ping:
win32-open3 requires Ruby version < 1.9.0.

经过进一步研究,我发现 1.9.X 尚无法使用 net-ping.有没有人有一段好的 ping 代码,他们愿意分享.

upon further research, I found that net-ping was not available yet for 1.9.X. Does anyone have a good piece of code that pings that they would be willing to share.

推荐答案

您可以随时执行此操作并使用正则表达式来解析结果或仅检查退出状态:

You can always do this and use regexps to parse the result or just check the exit status:

ping_count = 10
server = "www.google.com"
result = `ping -q -c #{ping_count} #{server}`
if ($?.exitstatus == 0) do
  puts "Device is up!"
end

您可以检查的 Ping 返回值:

Ping return values that you can check against:

如果从指定主机至少听到一个响应,ping 实用程序将返回退出状态为零;如果传输成功但未收到响应,则状态为 2;如果发生错误,或者其他值(来自 ).

The ping utility returns an exit status of zero if at least one response was heard from the specified host; a status of two if the transmission was successful but no responses were received; or another value (from <sysexits.h>) if an error occurred.

http://www.manpagez.com/man/8/ping

这篇关于1.9.1 的 ruby​​ ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆