
Kanji
User
/ Moderator
Sep 19, 2000, 4:26 PM
Post #2 of 9
(3105 views)
|
$host = "127.0.0.1"; or $host = "localhost.my.domain";. As for why it isn't working, you appear to have ignored the docs: the tcp/udp methods only work if the remote host is running the echo service, which any sensible admin will not. You want to use the icmp method, but that requires you to be root(like) in order for the ping to work. If you don't have root, then you can use the ping program as that usually executes with root privileges ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> my @ping = qw( /path/to/ping -c 1 ); sub ping { my $host = shift | | return; open( PING, "-|" ) or exec( @ping, $host ) or exit; return if eof( PING ); while ( <PING> ) { return 1 if /, (\d+)% packet loss/ && $1 < 100; } return 0; } print "$host is alive.\n" if ping( $host ); my $up = ping( $host ); if ( $up ) { print "YES" } elsif ( defined $up ) { print "NO" } else { print "ERR" }</pre><HR></BLOCKQUOTE> The last four lines show all the possibilities. The good thing about this method is you could define at what level of ping loss the host should be considered down. 100% is a definite, but depending on your situation, anything more than 50% may qualify as site down. An even simpler method would be ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> print "$host is alive.\n" if system( "/path/to/ping -c 1 $host 1>/dev/null 2>&1" ) == 0;</pre><HR></BLOCKQUOTE>
|