Problems with Net::Ping

Hello.
I’m trying to make a small program to ping an IP through TCP/ICMP but
when I run it I get an error.
D:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ping.rb:80:in
ping': no host specified (Argume ntError) from D:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/tcp.rb:38:inping’
from png.rb:50:in do_ping_tcp' from png.rb:19:ininitialize’
from png.rb:61:in new' from png.rb:61:in
The snip of code which ping works fine if its alone but something gets
messy with the rest of the code and I just don’t know what is it. Any
ideas?

require ‘rubygems’
require ‘net/ping’
include Net

class Ping
def initialize(*argv)
@argv = argv

if  @argv[0]=="help" || [email protected][0]
  send :help
end
if [email protected][1]
  @argv[1]="tcp"
end
# swap with case() later
if @argv[1] == "tcp"
  send :do_ping_tcp
elsif @argv[1] == "icmp"
  send :do_ping_icmp
end

end

def repeat_every(interval)
loop do
start_time = Time.now
yield
elapsed = Time.now - start_time
sleep([interval - elapsed, 0].max)
end
end

def help
puts <<-help
Commands:
help
end

def do_ping_tcp
Net::Ping::TCP.econnrefused = true
result=Net::Ping::TCP.new(‘192.168.1.1’,80)
if result.ping?
puts “alive…”
else
puts “dead…”
end

end

def do_ping_icmp
end
end
ping = Ping.new(*ARGV)

Fixed.
It was Include Net. Some kind of interference with the Net module
initialization maybe?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs