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

A

Athreya Vc

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,
 
B

brabuhr

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:
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
 
A

Athreya Vc

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,
 
B

brabuhr

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.)
 
A

Athreya Vc

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
 
A

Athreya Vc

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top