ruby ping为1.9.1

前端之家收集整理的这篇文章主要介绍了ruby ping为1.9.1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的红宝石代码中ping一个网站,看到net-ping是一个很好的图书馆.不幸的是,当我尝试gem安装net-ping我得到以下错误

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

经过进一步的研究,我发现net-ping还没有用于1.9.X.有没有人有一个很好的代码,他们会愿意分享.

解决方法

您可以随时执行此操作,并使用正则表达式来解析结果或仅检查退出状态:
ping_count = 10
server = "www.google.com"
result = `ping -q -c #{ping_count} #{server}`
if ($?.exitstatus == 0) do
  puts "Device is up!"
end

Ping返回值可以检查:

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

猜你在找的Ruby相关文章