Ruby:Ping.pingecho 缺失

发布于 2024-12-05 18:11:42 字数 688 浏览 1 评论 0原文

Ruby 曾经有一个 Ping.pingecho 方法,但似乎(和 Ping 模块)已经消失了:

% rvm use 1.8.7
Using ~/.rvm/gems/ruby-1.8.7-p334
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"'
true
% rvm use 1.9.2
Using ~/.rvm/gems/ruby-1.9.2-p180
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"'
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- ping (LoadError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
% ruby -e 'p Ping.pingecho "127.0.0.1"'
-e:1:in `<main>': uninitialized constant Object::Ping (NameError)

它是否已转移到不同的库(那么应该做什么?我需要加载它?),或者 它是否已被删除,并替换为不同的模块(那么我应该使用什么来确定IP是否可达?)。

Ruby used to have a Ping.pingecho method, but it seems as if (and the Ping module) have disappeared sometime:

% rvm use 1.8.7
Using ~/.rvm/gems/ruby-1.8.7-p334
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"'
true
% rvm use 1.9.2
Using ~/.rvm/gems/ruby-1.9.2-p180
% ruby -rping -e 'p Ping.pingecho "127.0.0.1"'
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- ping (LoadError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
% ruby -e 'p Ping.pingecho "127.0.0.1"'
-e:1:in `<main>': uninitialized constant Object::Ping (NameError)

Has it moved to a different library (so what should I require to load it?), or
has it been deleted, and replaced with a different module (so what should I use to determine whether a IP is reachable?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2024-12-12 18:11:42

不知道为什么或它去了哪里。 Rails 仍然有一个 Ping 类。稍作调整(使用类方法)将是:

require 'timeout'
require 'socket'

class Ping 
  def self.pingecho(host, timeout=5, service="echo")
    begin
      timeout(timeout) do
        s = TCPSocket.new(host, service)
        s.close
      end
    rescue Errno::ECONNREFUSED
      return true
    rescue   Timeout::Error, StandardError 
      return false 
    end
    return true
  end
end

p Ping.pingecho("127.0.0.1") #=> true
p Ping.pingecho("localhost") #=> true

Don know why or where it has gone. Rails still has a Ping class. A slight adaptation (to use a class method) would be:

require 'timeout'
require 'socket'

class Ping 
  def self.pingecho(host, timeout=5, service="echo")
    begin
      timeout(timeout) do
        s = TCPSocket.new(host, service)
        s.close
      end
    rescue Errno::ECONNREFUSED
      return true
    rescue   Timeout::Error, StandardError 
      return false 
    end
    return true
  end
end

p Ping.pingecho("127.0.0.1") #=> true
p Ping.pingecho("localhost") #=> true
明媚殇 2024-12-12 18:11:42

我刚刚遇到这个问题。我用 net-ping gem 作为替代品。 gems/net-ping-1.7.7/examples/example_pingtcp.rb 中有一个清晰的 TCP ping 示例:

p1 = Net::Ping::TCP.new(good, 'http')
p p1.ping?

rubydoc.info 链接 在撰写本文时不起作用,但这里是模块源代码 (tcp.rb) 中的有用注释,

# This method attempts to ping a host and port using a TCPSocket with
# the host, port and timeout values passed in the constructor.  Returns
# true if successful, or false otherwise.

所以我交换了这个:

return Ping.pingecho(server, 5, 22)

这样:

p = Net::Ping::TCP.new(server, 22, 5)
p.ping?

从旧的等效模块移动到新的等效模块有两个注意事项:

  1. 构造函数的参数是相反的(端口和超时)
  2. ping 的实际调用是通过调用 ping?< /code> 方法,而不仅仅是实例化。

I just encountered this issue. I settled with the net-ping gem as a replacement. It has a clear TCP ping example in gems/net-ping-1.7.7/examples/example_pingtcp.rb:

p1 = Net::Ping::TCP.new(good, 'http')
p p1.ping?

The rubydoc.info link at the time of this writing is not working, but here is a useful comment in the source of the module (tcp.rb)

# This method attempts to ping a host and port using a TCPSocket with
# the host, port and timeout values passed in the constructor.  Returns
# true if successful, or false otherwise.

So I swapped this:

return Ping.pingecho(server, 5, 22)

With this:

p = Net::Ping::TCP.new(server, 22, 5)
p.ping?

There are two caveats in moving from the old to the new equivalent module:

  1. The arguments to the constructor are reversed (port and timeout)
  2. The actual invocation of a ping is accomplished by calling the ping? method, rather than just instantiating.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文