http://www.perlmonks.org?node_id=359651

mike at rcn has asked for the wisdom of the Perl Monks concerning the following question:

Hi All:

A newbie to Perl, I have been wrestling with Ping for a couple of days now, and have completely run out of ideas.

In part of a process, I am pre-checking to ensure the remote servers are available, before modifying files and sending them to those servers.

My unit test includes the following rows in my ini file:

DBSERVER0 = \\PLYSDEV03\DEV03- DBSERVER1 =
My code strips the DBSERVERn value down to just the servername and pings it. (The second one intentionally has a blank value and should, therefore, fail the ping.)

BUT . . . both pings pass the test!

Following is the relevant code within the loop through the DBSERVERn keys in the ini section.I'm using use Net::Ping;

printf "$_: $dbSrvrUNC\n"; my $dbSrvrBox = $dbSrvrUNC; $dbSrvrBox =~ s/^(\\\\)//; # Get rid of any leading "\" + for UNC if ($dbSrvrBox =~ /\w+/) {$dbSrvrBox = $&}; # Save only the box name printf "DB Server to be tested for accessibility: $dbSrvrBox\n\n"; # Ensure the connection to the DB Server is available if (pingecho($dbSrvrBox, $timeout)) { &msgLog ("DB Server ($dbSrvrBox) is accessible at startup.\n"); printf "\n$dbSrvrBox is accessible at startup.\n\n"; } else { &msgLog ("Cannot access $dbSrvrBox - abending.\n\n"); &stdErr ("Cannot access $dbSrvrBox - abending.\n\n"); &abend ("Cannot access $dbSrvrBox "); };
##############################

Following is the related output:

DBSERVER0: \\PLYSDEV03\DEV03- DB Server to be tested for accessibility: PLYSDEV03 PLYSDEV03 is accessible at startup. DBSERVER1: DB Server to be tested for accessibility: is accessible at startup. Processing to be performed for:
Thanks in advance for your help!

Replies are listed 'Best First'.
Re: My ping should fail but does not
by Zaxo (Archbishop) on Jun 02, 2004 at 19:02 UTC

    You have left out the part that seems to be misbehaving. Where does pingecho() come from? What is its documented behavior? What does the code look like? It could be pinging localhost by default, which would (one hopes) return true.

    You may want to consider using Net::Ping and/or DBI.

    After Compline,
    Zaxo

      I was using Net::Ping; I'll update my question now.

      If I don't have a valid servername, will it default to the localhost? I didn't see anything about that in the Net-Ping-2.31 doc. . . .

        Ok, pingecho() is a deprecated back-compat wrapper for the ping method of Net::Ping. Running perldoc -m Net::Ping shows that ping returns an empty list if no $host is given. The default I mentioned was pure speculation. I don't see any way that an empty host string will return true, unless your inet_aton does so for empty argument.

        Try distinguishing between undef and 0 as return values for pingecho. It is supposed to return undef for an unresolved host and 0 for an unreachable one. That will help you diagnose the problem.

        How about just skipping the ping if the host string is not true?

        if ($host) { if (pingecho($host,$timeout)) { #... } }

        After Compline,
        Zaxo

Re: My ping should fail but does not
by robartes (Priest) on Jun 02, 2004 at 20:28 UTC
    A quick test with pingecho:
    $ perl -MNet::Ping -MData::Dumper -e 'my $retval=pingecho("");print Du +mper(\$retval);' $VAR1 = \undef;
    shows that if you call pingecho() with an empty host, it returns undef, which should fail your if.
    Are you sure that $dbSrvrUNC contains an empty string for the second ping?

    CU
    Robartes-