3 # Authors: karrer@bernina.ethz.ch (Andreas Karrer)
4 # pmarquess@bfsec.bt.co.uk (Paul Marquess)
9 @EXPORT = qw(ping pingecho);
12 use Socket 'PF_INET', 'AF_INET', 'SOCK_STREAM';
17 $Net::Ping::tcp_proto = (getprotobyname('tcp'))[2];
18 $Net::Ping::echo_port = (getservbyname('echo', 'tcp'))[2];
21 $Net::Ping::tcp_proto = $Net::Ping::tcp_proto ;
22 $Net::Ping::echo_port = $Net::Ping::echo_port ;
25 Carp::croak "ping not implemented yet. Use pingecho()";
31 Carp::croak "usage: pingecho host [timeout]"
32 unless @_ == 1 || @_ == 2 ;
34 my ($host, $timeout) = @_;
39 # check if $host is alive by connecting to its echo port, within $timeout
40 # (default 5) seconds. returns 1 if OK, 0 if no answer, 0 if host not found
42 $timeout = 5 unless $timeout;
44 if ($host =~ /^\s*((\d+\.){3}\d+)\s*$/)
45 { $ip = pack ('C4', split (/\./, $1)) }
47 { $ip = (gethostbyname($host))[4] }
49 return 0 unless $ip; # "no such host"
51 $saddr = pack('S n a4 x8', AF_INET, $Net::Ping::echo_port, $ip);
52 $SIG{'ALRM'} = sub { die } ;
57 return unless socket(PINGSOCK, PF_INET, SOCK_STREAM, $Net::Ping::tcp_proto) ;
58 return unless connect(PINGSOCK, $saddr) ;
73 Net::Ping, pingecho - check a host for upness
78 print "'jimmy' is alive and kicking\n" if pingecho('jimmy', 10) ;
82 This module contains routines to test for the reachability of remote hosts.
83 Currently the only routine implemented is pingecho().
85 pingecho() uses a TCP echo (I<not> an ICMP one) to determine if the
86 remote host is reachable. This is usually adequate to tell that a remote
87 host is available to rsh(1), ftp(1), or telnet(1) onto.
95 The remote host to check, specified either as a hostname or as an IP address.
99 The timeout in seconds. If not specified it will default to 5 seconds.
105 pingecho() uses alarm to implement the timeout, so don't set another alarm
106 while you are using it.