Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Issues with network connectivity and Net::Ping

by Steve_BZ (Chaplain)
on Jan 30, 2014 at 09:37 UTC ( [id://1072651]=perlquestion: print w/replies, xml ) Need Help??

Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

I have an active wireless connection on my Linux box (Kubuntu 13.10). When I manually ping Google with ping www.google.com at the command prompt I get the following output:

PING www.google.com (173.194.34.144) 56(84) bytes of data. 64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=1 t +tl=57 time=159 ms 64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=2 t +tl=57 time=190 ms 64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=3 t +tl=57 time=72.6 ms 64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=4 t +tl=57 time=199 ms 64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=5 t +tl=57 time=69.4 ms

But when I use Net::Ping in a perl script like this:

my $p = Net::Ping->new("tcp", 2); # With or without this next line. $p->port_number(getservbyname("http", "tcp")); if ($p->ping("www.google.com")) { print "Host is reachable\n"; return 1; } else { print "Host is not reachable\n"; return 0; }

I get "Host is not reachable" rather than the "Host is reachable" suggested by running ping manually.

What is happening here?

Thanks very much.

Steve.

Replies are listed 'Best First'.
Re: Issues with network connectivity and Net::Ping
by VinsWorldcom (Prior) on Jan 30, 2014 at 15:12 UTC

    I think your problem is 'getservbyname' is not returning what you think it is. This works fine for me (Win7 x64 / Strawberry 5.18.1):

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> cat test.pl use strict; use warnings; use Net::Ping; my $p = Net::Ping->new("tcp", 2); printf "GetServByName = %s\n", getservbyname("http", "tcp"); # Explicitly use the HTTP port number $p->port_number(80); if ($p->ping("www.google.com")) { print "Host is reachable\n"; } else { print "Host is not reachable\n"; } VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl GetServByName = http Host is reachable

      It's true. I just tested it and it works on Linux too.

      I'll time them both and see if I can get below the 500 ms that upset Karl so much. I think we should try to eliminate the interference of Solar Flares.

      Hi Guys,

      Well here are the timings (the code is at the bottom for you to critique):

      It is using a mobile router from three.com, so it may be less stable than a fibre optic connection.

      1) Here is a straight ping at t the command prompt (Range 75-400 ms):

      PING www.google.com (173.194.34.146) 56(84) bytes of data. 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=1 t +tl=55 time=394 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=2 t +tl=55 time=332 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=3 t +tl=55 time=362 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=4 t +tl=55 time=276 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=5 t +tl=55 time=92.4 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=6 t +tl=55 time=219 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=7 t +tl=55 time=116 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=8 t +tl=55 time=265 ms 64 bytes from lhr14s21-in-f18.1e100.net (173.194.34.146): icmp_seq=9 t +tl=55 time=75.9 ms ^C
      2) Here is the data from the programmatic pings:
      using Net::Ping (Range 500-1000 ms) 1031.06737136841 ms 729.610681533813 ms 1004.48346138 ms 775.990009307861 ms 600.036859512329 ms 589.807271957397 ms 754.30154800415 ms 605.571508407593 ms 560.036897659302 ms 782.053709030151 ms using Net::Ping::External (Range 400-800 ms) 791.388988494873 ms 570.100784301758 ms 687.773942947388 ms 402.096509933472 ms 430.928707122803 ms 398.749589920044 ms 712.586641311646 ms 837.354421615601 ms 560.385465621948 ms 650.154113769531 ms

      3) Here is the program:

      #!/usr/bin/perl -w use strict; use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock stat lstat ); use strict; use warnings; use Net::Ping; print "using Net::Ping\n"; my $p = Net::Ping->new("tcp", 2); $p->port_number(80); my $basetime = clock_gettime(); my $realtime = clock_gettime(); my $roundtrip = ($realtime-$basetime) * 1000; for (my $i=1; $i<=10 ; $i++){ if ($p->ping("www.google.com")) { $realtime = clock_gettime(); $roundtrip = ($realtime-$basetime) * 1000; print $roundtrip," ms\n"; } else { print "Host is not reachable\n"; } $basetime = clock_gettime(); } use Net::Ping::External qw(ping); print "using Net::Ping::External\n"; #my $p = Net::Ping->new("tcp", 2); #$p->port_number(80); $basetime = clock_gettime(); $realtime = clock_gettime(); $roundtrip = ($realtime-$basetime) * 1000; for (my $i=1; $i<=10 ; $i++){ if (ping(host => "www.google.com")) { $realtime = clock_gettime(); $roundtrip = ($realtime-$basetime) * 1000; print $roundtrip," ms\n"; } else { print "Host is not reachable\n"; } $basetime = clock_gettime(); }

      I'm interested in your opinions.

      Actually everything takes too long. Rather than doing it synchronously, I'm thinking of setting a global variable which I set from a background loop every ten seconds. That way the information is recent, but i don't need to wait for it.

      Then I'll check the variable not the ping.

      Regards

      Steve

Re: Issues with network connectivity and Net::Ping
by vinoth.ree (Monsignor) on Jan 30, 2014 at 11:55 UTC

    Could you please try with different protocol instead of 'tcp'. like try with 'icmp' or 'udp' and check.


    All is well

      If I recall correctly, the ICMP protocol requires root access, this could be the reason he's trying to use TCP.

      I ended up using "/bin/ping" for my own network monitoring script and I believe this was one of the reasons. I also recall having problems with Net::Ping not timing out properly in some situations which can be a real problem when you have more than a handful of hosts to check.

      -- FloydATC

      Time flies when you don't know what you're doing

      Hi vinoth.ree,

      I tried with udp and I still get a timeout.

      FloydATC is correct in supposing that I didn't use ICMP to avoid the need to be root.

      I'm thinking of going down the direct call route. What do you think?

      Like this

      my $p = `ping -c 1 www.google.com | grep -i "bytes.*ms"`
      And then I either get:
      64 bytes from lhr14s21-in-f16.1e100.net (173.194.34.144): icmp_seq=1 t +tl=55 time=549 ms

      or ..

      ping: unknown host www.google.com

      There is a slight delay. But it seems reliable. Any views?

      Regards

      Steve

        Potentially, Net::Ping::External, together with the 'external' protocol should accomplish the same.

        IMHO 549ms isn't slight.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1072651]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found