Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Short pinging script isn't working

by Anonymous Monk
on Nov 21, 2007 at 16:05 UTC ( [id://652159]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use warnings; use strict; use Net::Ping; my $target = "10.64.164.56"; my $p = Net::Ping->new(); if ($p->ping($target, 2)) { print "We can ping"; } else { print "no"; }
It results in "no" when I can ping the local IP via CMD (Windows). Ultimately I want to ping every 5 seconds and add a timeout of 2 seconds (if someone can show me how).

Replies are listed 'Best First'.
Re: Short pinging script isn't working
by jbert (Priest) on Nov 21, 2007 at 16:12 UTC
    The docs for Net::Ping say that it supports various different ways of checking a host is alive, and by default uses 'TCP', which attempts to connect to the TCP 'echo' port of the IP address you specify.

    This is unlikely to work in a modern network environment (and is a different test to the ICMP ping your command line tool uses).

    Try specifying the 'icmp' protocol and see if that helps. The docs say that this is the first arg to the ctor, so perhaps:

    my $p = Net::Ping->new('icmp');
    The ctor also takes other arguments (including a timeout), which may be of interest to you.
      Try specifying the 'icmp' protocol

      Be aware that only the super-user (root) is allowed to issue ICMP packets. If you look at the ping binary on a Unix system, you will see that it is setuid root. Thus, a regular user issuing an icmp request from Perl-space is unlikely to receive a reply.

      In any event, ping should not be used to determine whether the target machine is alive and well. It doesn't do anything more than prove that the path from here to there is functional. But the thing at the other end may be well and truly wedged. Better to check whether it responds sensibly to an application transaction, like a HEAD / sent to port 80 and see what comes back.

      • another intruder with the mooring in the heart of the Perl

        Right, ICMP pings are useful mainly to determine if there is a network path available (and maybe to get a handle on your latency, do discoveries, etc. ...). If you look at how Nagios works (by default), it does application level checking normally, and then when that fails it starts running ICMP pings to try to determine if you're having a network problem or an application problem. So really, you need to use both kinds of things in tandem to get to your root cause.

Re: Short pinging script isn't working
by andyford (Curate) on Nov 21, 2007 at 17:52 UTC

    I ran this on Windows (XP Pro) with ActiveState Perl 5.8.8 and it worked just by changing the target to my local host. Do you have the Windows firewall running?

    As far as the timeout goes, you already have it in your call to the 'ping' function:

    $p->ping($target, 2)
    gives a 2 second timeout. Or as jbert pointed out ('ctor' means 'constructor' means Net::Ping->new), you can specify the timeout in your 'new' call.

    To repeat every 5 seconds I think you have to get outside Net::Ping. Here's a rough untested estimate of one way to do it without even trying to take into account the time that it actually takes to do the ping:

    while (1) { $p->ping($target, 2); sleep 5; }

    After that, if you want to initiate the ping on every fifth second you could try Schedule::Cron. Then, when you get tired of reinventing the monitoring wheel, install Nagios for some serious pinging that can result in an annoying sound coming from your pager at 2AM.

Re: Short pinging script isn't working
by Aim9b (Monk) on Nov 21, 2007 at 19:19 UTC
    grinder is correct. ping is a poor way to test for any communications. All you basically know is that the machine has power to the NIC. There may or may not even be a TCP/IP application running. We have been burned on several occasions. Now we use an overkill ftp session. I'd be interested in knowing how you finally work this out. Thanks.
Re: Short pinging script isn't working
by rb1980 (Initiate) on Nov 21, 2007 at 22:03 UTC
    I seem to recall having a similar issue in the past. IIRC, it had to do with the fact that windows uses a udp ping instead of icmp. Try changing this line:
    my $p = Net::Ping->new("icmp");
    also, if you have multiple interfaces, force it to bind to one:
    $p->bind("10.64.164.nnn");
    hope that helps.

Log In?
Username:
Password:

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

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

    No recent polls found