5 Net::Ping, pingecho - check a host for upness
10 print "'jimmy' is alive and kicking\n" if pingecho('jimmy', 10) ;
14 This module contains routines to test for the reachability of remote hosts.
15 Currently the only routine implemented is pingecho().
17 pingecho() uses a TCP echo (I<not> an ICMP one) to determine if the
18 remote host is reachable. This is usually adequate to tell that a remote
19 host is available to rsh(1), ftp(1), or telnet(1) onto.
27 The remote host to check, specified either as a hostname or as an IP address.
31 The timeout in seconds. If not specified it will default to 5 seconds.
37 pingecho() uses alarm to implement the timeout, so don't set another alarm
38 while you are using it.
42 # Authors: karrer@bernina.ethz.ch (Andreas Karrer)
43 # pmarquess@bfsec.bt.co.uk (Paul Marquess)
48 @EXPORT = qw(ping pingecho);
53 $tcp_proto = (getprotobyname('tcp'))[2];
54 $echo_port = (getservbyname('echo', 'tcp'))[2];
57 croak "ping not implemented yet. Use pingecho()";
63 croak "usage: pingecho host [timeout]"
64 unless @_ == 1 || @_ == 2 ;
66 local ($host, $timeout) = @_;
71 # check if $host is alive by connecting to its echo port, within $timeout
72 # (default 5) seconds. returns 1 if OK, 0 if no answer, 0 if host not found
74 $timeout = 5 unless $timeout;
76 if ($host =~ /^\s*((\d+\.){3}\d+)\s*$/)
77 { $ip = pack ('C4', split (/\./, $1)) }
79 { $ip = (gethostbyname($host))[4] }
81 return 0 unless $ip; # "no such host"
83 $saddr = pack('S n a4 x8', AF_INET, $echo_port, $ip);
84 $SIG{'ALRM'} = sub { die } ;
90 unless socket(PINGSOCK, PF_INET, SOCK_STREAM, $tcp_proto) ;
93 unless connect(PINGSOCK, $saddr) ;