我正在试图弄清楚如何使用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
如果有人可以帮助我,我将不胜感激,谢谢
答案 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"
匹配结果true
为nil
,if nil
为false。所以,你的第二个例子将始终输出&#34; true&#34; (即使.ping?
为false
)。
和&#39;管理员权限&#39;:您无法像普通用户一样创建ICMP数据包。除非您使用SUID的/bin/ping
。 (net/ping
宝石不使用)。 `。