3 # Authors: karrer@bernina.ethz.ch (Andreas Karrer)
4 # pmarquess@bfsec.bt.co.uk (Paul Marquess)
9 @EXPORT = qw(ping pingecho);
14 $tcp_proto = (getprotobyname('tcp'))[2];
15 $echo_port = (getservbyname('echo', 'tcp'))[2];
18 croak "ping not implemented yet. Use pingecho()";
24 croak "usage: pingecho host [timeout]"
25 unless @_ == 1 || @_ == 2 ;
27 local ($host, $timeout) = @_;
32 # check if $host is alive by connecting to its echo port, within $timeout
33 # (default 5) seconds. returns 1 if OK, 0 if no answer, 0 if host not found
35 $timeout = 5 unless $timeout;
37 if ($host =~ /^\s*((\d+\.){3}\d+)\s*$/)
38 { $ip = pack ('C4', split (/\./, $1)) }
40 { $ip = (gethostbyname($host))[4] }
42 return 0 unless $ip; # "no such host"
44 $saddr = pack('S n a4 x8', AF_INET, $echo_port, $ip);
45 $SIG{'ALRM'} = sub { die } ;
51 unless socket(PINGSOCK, PF_INET, SOCK_STREAM, $tcp_proto) ;
54 unless connect(PINGSOCK, $saddr) ;