Dear All
I am just want to write a program to ping my servers if them are alive,
using Ruby, but it doesnt work, whatever the ip address I put down in
the programe, it all said ok, any advise would be great appricated.
my PC os is windows7 64digital
require ‘net/ping’
include Net
Ping::TCP.service_check = true
loocaalhost=“192.168.1.555”
if Net::Ping::TCP.new(‘loocaalhost’)
puts ‘TCP OK’
else
puts ‘NO respons’
end
if Net::Ping::External.new(‘loccalhost’)
puts ‘Exteranl OK’
else
puts ‘NO respons’
end
TCP OK
Exteranl OK
Just change:
Net::Ping::TCP.new(‘loocaalhost’)
to:
Net::Ping::TCP.new(loocaalhost)
And you might want to consider a different spelling of “loocaalhost” as
well, but you don’t want to quote your variable.
From my iPhone
Pat
Thank you Pat for your kindly help.
I did , but it still work wrong.
I test it in irb as the following:
require “net/ping”
=> true
puts ‘OK’ if Net::Ping::External.new ‘192.168.1.1’
OK
=> nil
puts ‘OK’ if Net::Ping::External.new ‘192.168.1.5’
OK
=> nil
puts ‘OK’ if Net::Ping::External.new ‘192.168.1.555’
OK
=> nil
what a amazing, the 199.168.1.555 can be ping and is ok ?!
I use Ruby 1.9.2-260, win7 64. net-ping 1.5.1, kabasky fire wall in
local PC.
I used msdos ping command and captured the stdout and it works just
fine.
$msdos=“ping “[email protected] #construct the msdos command
result=eval(”"+$msdos+"
”) #notice the eval statement
This is before I became aware of net/ping
Hope that helps
Joe
On Mon, Oct 3, 2011 at 2:49 PM, Edward QU [email protected] wrote:
Dear All
I am just want to write a program to ping my servers if them are alive,
using Ruby, but it doesnt work, whatever the ip address I put down in
the programe, it all said ok, any advise would be great appricated.
Code from an old project:
def ping hosts
netstatus = {}
threads = ThreadGroup.new
PingTCP.econnrefused = true
hosts = [hosts] unless hosts.respond_to?(:each)
hosts.each{ |host|
threads.add Thread.new(host){ |h|
unless h.hostname.to_s.size < 1
ping_obj = PingTCP.new(h.hostname.to_s, 22, 1)
if ping_obj.ping?
# Success pinging 'host'
netstatus[h.id] = "online"
else
# Failed to ping 'host'
netstatus[h.id] = "offline"
end
else
netstatus[h.id] = ""
end
}
}
threads.list.each { |t| t.join }
netstatus
end
thanks all above reply,
the Net::Ping::External could not work, but the Net::Ping::TCP. works.
pt = Net::Ping::TCP.new("#{ip_list}")
if pt.ping
# puts “#{ip_list} TCP ping successful”
else
# puts “ping unsuccessful: #{pt.exception}”
end
Regards
Edward
according to the docs
(http://rubyforge.org/docman/view.php/735/296/ping_txt.html)
you need to call the ping method on the object.
You are just creating the object
cheers