in reply to Re: Net:Ping HELP!
in thread Net:Ping HELP!

sorry i should have posted the actual script. here it is. It is being run as root on a FreeBSD 4.7 box.


It takes about 1 second to run this script, but then I get no output at all.

#!/usr/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping=Net::Ping->new(); while ($ping->ping($host,1)) { print "down\n"; }

Replies are listed 'Best First'.
Re: Re: Re: Net:Ping HELP!
by JamesNC (Chaplain) on May 29, 2003 at 05:24 UTC
    You left out what protocol to use... try this snippet
    #!/perl/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping=Net::Ping->new("icmp"); if ($ping->ping($host,1)){ print "$host is alive"; }else{ print "$host unreachable" }

    Update: added else statement
      It needed a little cleaning up but it does work. ;-)

      #!/usr/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping = Net::Ping->new("udp"); if ($ping->ping($host,1)){ print "$host is alive\n"} else{ print "$host is unreachable\n" };

      I could swear on my life that i tried putting in "icmp" and it didn't make a difference, however I tried you snippet and it worked perfectly!!!! Thank you so much.


      I have also come up with an alternative way of doing this without using Net::Ping, although I have heard that it is better to use Net::Ping for various reasons. for those who are interested, see my code below.


      your code
      #!/usr/bin/perl -w use strict; use Net::Ping; my $host = "127.0.0.1"; my $ping=Net::Ping->new("icmp"); if ($ping->ping($host,2)){ print "$host is alive"; }else{ print "$host unreachable" }

      My alternative code (very sloppy but it did the trick)
      #!/usr/bin/perl -w use strict; checkup(); sub checkup { my $host = "209.94.100.100"; my $packloss = `ping -c 1 -s 1 $host | grep loss | awk '{print \$7}'`; $packloss = substr($packloss,0,length($packloss) -1); if ($packloss ne "100%") { print "$host is up with $packloss packet loss\n"; }else { print "$host is down with $packloss packet loss\n"; }