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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Net:Ping HELP!
by daeve (Deacon) on May 29, 2003 at 07:31 UTC
    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" };
Re: Re: Re: Re: Net:Ping HELP!
by Anonymous Monk on May 29, 2003 at 07:44 UTC

    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"; }