Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
go ahead... be a heretic
 
PerlMonks  

Module Net-Ping

by Kozz (Friar)
on Sep 23, 2002 at 16:49 UTC ( #200159=perlquestion: print w/ replies, xml ) Need Help??
Kozz has asked for the wisdom of the Perl Monks concerning the following question:

Most esteemed monks: I wanted to write a sort of "wrapper" script for my crontab that calls fetchmail to ping an external host before attempting to fetch the mail, since my cable network connectivity can often be quite flaky. When it's unreachable, my mailbox fills up with fetchmail error messages. So I decided to use the Net::Ping module, but it's not quite working as expected.
I wrote this simple script to test things:
#!/usr/bin/perl -w use strict; use Net::Ping; my $fetchmail = '/usr/local/bin/fetchmail'; my @fetchmail_args = ('-s', '-t', '60'); my $hostname = 'hostname'; # an external host my $timeout = 3; my $p = Net::Ping->new('udp', $timeout); if( $p->ping($hostname) ){ print "$hostname is alive.\n"; # exec($fetchmail, @fetchmail_args); }else{ print "$hostname is unreachable.\n"; }
Then within the if/else construct I would place an exec() call (to fetchmail with the desired parameters once I knew the ping portion was working correctly.

However, when putting the external hostname in $hostname, it always returns "unreachable", yet I can still use /bin/ping to reach it just fine. So I tried for a $hostname also 'localhost', '127.0.0.1', and also the local interface's IP address, '10.0.0.1'. But they also all return unreachable.

What am I doing wrong? It must be something quite simple... Thank you for your consideration.

--Kozz

Comment on Module Net-Ping
Download Code
Re: Module Net-Ping
by Preceptor (Chaplain) on Sep 23, 2002 at 16:54 UTC
    At a guess, it's because you are using UDP for your ping.
    If you aim a UDP packet at a 'closed' port, then you get an ICMP 'destination port unreachable' (OTOH).
    It means, that it could reach the host, but the port in question wasn't open. Suggest you either try a 'known good' udp port (hard on a mailserver) or use TCP or ICMP instead.
    HTH
    --
    It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.
      Preceptor: Thank you for the reply. I chose "UDP" because it is the Net::Ping's default. "TCP" likewise returns unreachable.

      If I change it to 'icmp', it DOES work, but only if effective uid is root (per the module's documentation). I wanted to run this w/o root privs, just as a plain user.

      There are indeed other services available on this server, but I don't see in the Net::Ping documentation any way to specify a different port number from the default ('echo'). Perhaps the only thing I can do is run it as root or setuid the script. But any other advice would be helpful.

      Thank you!

        I'd suggest trying using netcat to test whether echo/tcp and echo/udp are actually available.
        Otherwise, you are probably looking at implementing a similar functionality using Net::Telnet (using the POP port)
        I posted a CUFP here. From which you could probably quite easily nab the 'OpenConn' sub to do what you want.
        --
        It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.

        On CPan there is a Net::Ping::External that relies on the binary ping command that does not need root level permissions (I presume so long as your ping command doesn't)

        It also works on a wide variety of systems

        Check it out!

        --blm--

        It's seems weird to be advocating another non-Perl solution in so short a timespan, but this is something done obscenely easy with a shell script if your ping binary has appropriate permissions (they usually do)...

        #!/bin/sh PING='/bin/ping -c 5' # FreeBSD/Linux #PING='/usr/sbin/ping' # Solaris FETCHMAIL='/usr/local/bin/fetchmail -s -t 60' $PING $hostname 1>/dev/null 2>&1 \ && $FETCHMAIL

        If you absolutely must write something in Perl, then I second blm's suggestion of Net::Ping::External (alt.).

            --k.


Re: Module Net-Ping
by Enlil (Parson) on Sep 23, 2002 at 17:06 UTC
    Try changing my $p = Net::Ping->new('udp', $timeout);
    to
    my $p = Net::Ping->new('tcp', $timeout);
    or
    my $p = Net::Ping->new('icmp', $timeout);
    if you can set the setuid to root (with the icmp).

    -Enlil

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-04-20 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls