Forum: Ruby Net::Ping:Class (NoMethodError)

Ad61766755448df27e1183e493055d6c?d=identicon&s=25 Bal Sidhu (balvirs)
on 2013-09-10 18:14
Hi,
can someone pls help and suggest the workaround, when I run the
following code, it return error

require 'rubygems'
require 'net/ping'

class PingIp

def ping_system(ping_system)
    @ping_address = "ping_system"
      #result = Ping.pingecho(ip_address,5,80)
      result = Net::Ping.ping(@ping_address, 5)
if result
    puts "system is alive"
else
    puts "system is offline"
end
      return result
  end
  end # end of class

  instance = PingIp.new
  instance.ping_system('10.55.89.66')

Error message

undefined method `ping' for Net::Ping:Class (NoMethodError)

I am using ruby 1.9.2

Thanks
E2cc2f0e2348266c536838e4e9f67a74?d=identicon&s=25 Eliezer Croitoru (Guest)
on 2013-09-10 23:23
(Received via mailing list)
This is an example for ICP ping but I assume you will want a http ping
perhaps?

#!/usr/local/bin/ruby

require 'net/ping'

host = ARGV[0]


u = Net::Ping::ICMP.new(host)

def pingable?(addr, u)
  #output = `ping -c 4 #{addr}`
  #[(!output.include? "100% packet loss"), output]
  [u.ping?,"some output"]
end

res = pingable?(host, u)
if res[0]
  #puts "OK: #{res[1]}"
  puts "OK: Works fine!"
  exit 0
else
  #puts "Critical: #{res[1]}"
  puts "Critical: Has issues!"
  exit 1
end
#END

you can use the net ping for ports etc...
an http ping is like:
#!/usr/local/bin/ruby

require 'net/ping'

host = ARGV[0]


u = Net::Ping::HTTP.new(host)

def pingable?(addr, u)
  #output = `ping -c 4 #{addr}`
  #[(!output.include? "100% packet loss"), output]
  [u.ping?,"some output"]
end

res = pingable?(host, u)
if res[0]
  #puts "OK: #{res[1]}"
  puts "OK: Works fine!"
  exit 0
else
  #puts "Critical: #{res[1]}"
  puts "Critical: Has issues!"
  exit 1
end
#END

hope it helps.

Eliezer
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.