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

I have a script that does ping on my Solaris 7 OS but I need it to email me if an IP address does not ping.

I tried to use the Unix exit status to find invalid ip addresses ($? = EXIT STATUS...IF 0 THEN IT SUCCEEDED, IF OTHER THEN FAILED) then I would be able to email myself if an IP address didnt ping. My exit status check is not working. Anyone have a better idea of how I could do this??
#!/usr/local/bin/perl @a = (<DATA>); $s = system("$?"); foreach (@a) { system("/usr/sbin/ping $_"); print system("$?\n"); if ( $? != 0) { print "Could not ping host."; } else { print "Ping successful.\n"; } } close(DATA); __DATA__ 111.111.111.111 222.222.222.333 232.454.556.565

Replies are listed 'Best First'.
Re: My Ping attempt in perl
by DamnDirtyApe (Curate) on Jul 05, 2002 at 18:10 UTC
      The problem with Net::Ping is that the "icmp protocol requires that the program be run as root or that it be setuid to root" (from perldoc Net::Ping). Otherwise, it will try tcp or udp connections to the remote system's "echo" port. Many systems do not have an echo server running, which would make them seem unreachable, even when they are not. The system's "ping" command should work fine, anyway, given the corrections made by others in this node.
Re: My Ping attempt in perl
by grinder (Bishop) on Jul 05, 2002 at 20:44 UTC
    I recommend avoiding the use of ping, and instead attempting to see whether anyone answers the port the service you are interested in is registered (25 for email, 80 for web, 1352 for Lotus and so on).

    Your server may have had problems, panicked and/or rebooted into single user mode, the kernel may still be speaking to the network interfaces, but not much else may be happening.

    Excuse me for sounding like a broken record on this issue.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      Definitely should check the services but ping can still helpful to see if it's reachable or not (Assuming you know they don't filter it). The alarms I have written consist of pinging and http-fetching, etc. In my situation it helps isolate the problem. (Especially if I'm on the road.)

      -Lee

      "To be civilized is to deny one's nature."
Re: My Ping attempt in perl
by flounder99 (Friar) on Jul 05, 2002 at 20:00 UTC
    Change
    print system("$?\n");
    to
    print "$?\n";
    You are calling a program that is the return value of ping! (probably "0") so in your if statement you are testing the return value of a program called "0" which is probably not what you want.

    --

    flounder

Re: My Ping attempt in perl
by Anonymous Monk on Jul 05, 2002 at 18:18 UTC
    Try this...
    my $output = system("/bin/ping -c 1 2>&1> /dev/null $HOST"); if ($output) { print "Could not ping host."; } else { print "Ping successful.\n"; }
      Thanks for all your replies! I will try all the information you all gave me first thing this Monday morning.
      Dont forget (like I did) to change $HOST to $_
Re: My Ping attempt in perl
by cybear (Monk) on Jul 05, 2002 at 19:44 UTC
    This may be more complicated than what you are looking for,
    but check out WhichHost AKA pingtest.pl
    I ping $available_firewall using `ping` and check for
    a pattern match in the results.