Ruby net-ping使用混乱

时间:2016-03-20 19:21:10

标签: ruby ping

我正在试图弄清楚如何使用net/ping宝石,自从我使用它以来,我有几个问题:

  • 为什么您需要管理员权限才能运行ICMP
  • 它返回true还是false?例如:

    Net::Ping::ICMP.new("127.0.0.1").ping?# <= true 
    
    #So I could do something like this:
    ping = Net::Ping::ICMP.new("127.0.0.1") 
    if ping == true
      puts "true"
    else
      puts "This shouldn't run"
    end
    #<= true
    
    #or does it return packets received?
    #so I could do something like this:
    
    ping = Net::Ping::ICMP.new("127.0.0.1")ping?
    if ping =~ /Received = 1/
      puts "true"
    else
      puts "This shouldn't run"
    end
    #<= true
    

我很困惑,我似乎无法在Google上找到与其工作原理相关的任何内容:https://www.google.com/search?safe=off&btnG=Search&q=ruby+net%3A%3Aping%3A%3Aicmp+tutorial

如果有人可以帮助我,我将不胜感激,谢谢

1 个答案:

答案 0 :(得分:1)

这就是ruby eval if

的方式
irb> ping = true
 => true 
irb > ping =~ /Received = 1/
 => nil 
irb> nil ? "NIL is true" : "NIL is not true"
 => "NIL is not true" 

匹配结果truenilif nil为false。所以,你的第二个例子将始终输出&#34; true&#34; (即使.ping?false)。

和&#39;管理员权限&#39;:您无法像普通用户一样创建ICMP数据包。除非您使用SUID的/bin/ping。 (net/ping宝石不使用)。 `。