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