Net-ping error says "false" though server can be reached

Hi All,

I am a newbie to Ruby programming.

I am trying a do a ping check on a server

irb(main):006:0> HOST='192.168.1.2'
=> "192.168.1.2'"

irb(main):007:0> Ping.pingecho(HOST)
=> false

where as I can ping the server for the host where I am running the irb.

ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]

gem list --local

*** LOCAL GEMS ***

hoe (2.8.0)
net-ping (1.3.7)
net-ssh (2.0.23)
rake (0.8.7)

Am I missing something here?

Please help

Regards,

···

--
Posted via http://www.ruby-forum.com/.

I am trying a do a ping check on a server

irb(main):006:0> HOST='192.168.1.2'
=> "192.168.1.2'"

irb(main):007:0> Ping.pingecho(HOST)
=> false

Am I missing something here?

It would help us see what's wrong if you included [more] complete
code, but I've been using something like this:

cat p.rb

require 'rubygems'
require "net/ping"

def ping host
  Net::Ping::TCP.econnrefused = true
  ping_obj = Net::Ping::TCP.new(host, 22, 1)
  ping_obj.ping?
end

p ping(ARGV[0])

ruby p.rb localhost

true

ruby p.rb foozle

false

ping -c 1 foozle

ping: unknown host foozle

ruby p.rb ********

true

ping -c 1 ********

1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.146/0.146/0.146/0.000 ms

This type of TCP ping breaks down if I try to ping outside the firewall:

ruby p.rb www.yahoo.com

false

ping -c 1 www.yahoo.com

--- ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 36.570/36.570/36.570/0.000 ms

···

On Thu, Dec 23, 2010 at 3:28 AM, Athreya Vc <athreyavc@yahoo.co.in> wrote:

Hi,

I trying this

irb(main):003:0> host='192.168.1.1'
=> "192.168.1.1"
irb(main):004:0> tcp = Net::Ping::TCP.new(host)
=> #<Net::Ping::TCP:0xb717377c @duration=nil, @warning=nil, @timeout=5,
@exception=nil, @host="192.168.1.1", @port=7>
irb(main):005:0> tcp.ping?
=> false

BUT,

ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_req=1 ttl=64 time=1.00 ms
64 bytes from 192.168.1.1: icmp_req=2 ttl=64 time=1.00 ms

I am confused.

Regards,

···

--
Posted via http://www.ruby-forum.com/.

Sorry Again,

This worked for me

irb(main):012:0> puts 'OK' if Net::Ping::External.new '192.168.1.1'
OK
=> nil
irb(main):013:0>

I am trying to write a script and kind of went impatient,

But the syntax is really cool, it is almost English,

Thanks for your inputs.

Regards

···

--
Posted via http://www.ruby-forum.com/.

irb(main):013:0> Net::Ping::TCP.econnrefused = true
=> true
irb(main):014:0> o = Net::Ping::TCP.new '192.168.1.1'
=> #<Net::Ping::TCP:0xb70f681c @duration=nil, @warning=nil, @timeout=5,
@exception=nil, @host="192.168.1.1", @port=7>
irb(main):016:0> o.ping?
=> true

Works!!

We are considering "econnrefused" to be successful Ping

Thanks a Lot for the quick response.

Regards,

···

--
Posted via http://www.ruby-forum.com/.

I trying this

irb(main):003:0> host='192.168.1.1'
=> "192.168.1.1"
irb(main):004:0> tcp = Net::Ping::TCP.new(host)
=> #<Net::Ping::TCP:0xb717377c @duration=nil, @warning=nil, @timeout=5,
@exception=nil, @host="192.168.1.1", @port=7>
irb(main):005:0> tcp.ping?
=> false

Is that host listening on TCP port 7? If not, try setting
Net::Ping::TCP.econnrefused = true

BUT,

ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_req=1 ttl=64 time=1.00 ms
64 bytes from 192.168.1.1: icmp_req=2 ttl=64 time=1.00 ms

I am confused.

My guess of what's happening here is this:

Ping::TCP tries to connect to port 7 on 1.1
  1.1 is not listening on port 7
    1.1 sends a connection refused message
      Ping::TCP reports false because the port is closed

(If there is a firewall running on 1.1 that could make the situation
more complex though.)