2 Net::Ping - check a remote host for reachability
4 $Id: Ping.pm,v 1.1 2002/06/04 00:41:52 rob Exp $
10 print "$host is alive.\n" if $p->ping($host);
13 $p = Net::Ping->new("icmp");
14 $p->bind($my_addr); # Specify source interface of pings
15 foreach $host (@host_array)
18 print "NOT " unless $p->ping($host, 2);
24 $p = Net::Ping->new("tcp", 2);
25 # Try connecting to the www port instead of the echo port
26 $p->{port_num} = getservbyname("http", "tcp");
27 while ($stop_time > time())
29 print "$host not reachable ", scalar(localtime()), "\n"
30 unless $p->ping($host);
35 # High precision syntax (requires Time::HiRes)
36 $p = Net::Ping->new();
38 ($ret, $duration, $ip) = $p->ping($host, 5.5);
39 printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration)
43 # For backward compatibility
44 print "$host is alive.\n" if pingecho($host);
47 This module contains methods to test the reachability of remote hosts on
48 a network. A ping object is first created with optional parameters, a
49 variable number of hosts may be pinged multiple times and then the
52 You may choose one of four different protocols to use for the ping. The
53 "udp" protocol is the default. Note that a live remote host may still
54 fail to be pingable by one or more of these protocols. For example,
55 www.microsoft.com is generally alive but not pingable.
57 With the "tcp" protocol the ping() method attempts to establish a
58 connection to the remote host's echo port. If the connection is
59 successfully established, the remote host is considered reachable. No
60 data is actually echoed. This protocol does not require any special
61 privileges but has higher overhead than the other two protocols.
63 Specifying the "udp" protocol causes the ping() method to send a udp
64 packet to the remote host's echo port. If the echoed packet is received
65 from the remote host and the received packet contains the same data as
66 the packet that was sent, the remote host is considered reachable. This
67 protocol does not require any special privileges. It should be borne in
68 mind that, for a udp ping, a host will be reported as unreachable if it
69 is not running the appropriate echo service. For Unix-like systems see
70 the inetd(8) manpage for more information.
72 If the "icmp" protocol is specified, the ping() method sends an icmp
73 echo message to the remote host, which is what the UNIX ping program
74 does. If the echoed message is received from the remote host and the
75 echoed information is correct, the remote host is considered reachable.
76 Specifying the "icmp" protocol requires that the program be run as root
77 or that the program be setuid to root.
79 If the "external" protocol is specified, the ping() method attempts to
80 use the `Net::Ping::External' module to ping the remote host.
81 `Net::Ping::External' interfaces with your system's default `ping'
82 utility to perform the ping, and generally produces relatively accurate
83 results. If `Net::Ping::External' if not installed on your system,
84 specifying the "external" protocol will result in an error.
88 Net::Ping->new([$proto [, $def_timeout [, $bytes]]]);
89 Create a new ping object. All of the parameters are optional. $proto
90 specifies the protocol to use when doing a ping. The current choices
91 are "tcp", "udp" or "icmp". The default is "udp".
93 If a default timeout ($def_timeout) in seconds is provided, it is
94 used when a timeout is not given to the ping() method (below). The
95 timeout must be greater than 0 and the default, if not specified, is
98 If the number of data bytes ($bytes) is given, that many data bytes
99 are included in the ping packet sent to the remote host. The number
100 of data bytes is ignored if the protocol is "tcp". The minimum (and
101 default) number of data bytes is 1 if the protocol is "udp" and 0
102 otherwise. The maximum number of data bytes that can be specified is
105 $p->source_verify( { 0 | 1 } );
106 Allows source endpoint verification to be enabled or disabled. This
107 is useful for those remote destinations with multiples interfaces
108 where the response may not originate from the same endpoint that the
109 original destination endpoint was sent to.
111 This is enabled by default.
113 $p->hires( { 0 | 1 } );
114 Causes this module to use Time::HiRes module, allowing milliseconds
115 to be returned by subsequent calls to ping().
117 This is disabled by default.
119 $p->bind($local_addr);
120 Sets the source address from which pings will be sent. This must be
121 the address of one of the interfaces on the local host. $local_addr
122 may be specified as a hostname or as a text IP address such as
125 If the protocol is set to "tcp", this method may be called any
126 number of times, and each call to the ping() method (below) will use
127 the most recent $local_addr. If the protocol is "icmp" or "udp",
128 then bind() must be called at most once per object, and (if it is
129 called at all) must be called before the first call to ping() for
132 $p->ping($host [, $timeout]);
133 Ping the remote host and wait for a response. $host can be either
134 the hostname or the IP number of the remote host. The optional
135 timeout must be greater than 0 seconds and defaults to whatever was
136 specified when the ping object was created. Returns a success flag.
137 If the hostname cannot be found or there is a problem with the IP
138 number, the success flag returned will be undef. Otherwise, the
139 success flag will be 1 if the host is reachable and 0 if it is not.
140 For most practical purposes, undef and 0 and can be treated as the
141 same case. In array context, the elapsed time is also returned. The
142 elapsed time value will be a float, as retuned by the
143 Time::HiRes::time() function, if hires() has been previously called,
144 otherwise it is returned as an integer.
147 When you are using the stream protocol, this call pre-opens the tcp
148 socket. It's only necessary to do this if you want to provide a
149 different timeout when creating the connection, or remove the
150 overhead of establishing the connection from the first ping. If you
151 don't call `open()', the connection is automatically opened the
152 first time `ping()' is called. This call simply does nothing if you
153 are using any protocol other than stream.
156 Close the network connection for this ping object. The network
157 connection is also closed by "undef $p". The network connection is
158 automatically closed if the ping object goes out of scope (e.g. $p
159 is local to a subroutine and you leave the subroutine).
161 pingecho($host [, $timeout]);
162 To provide backward compatibility with the previous version of
163 Net::Ping, a pingecho() subroutine is available with the same
164 functionality as before. pingecho() uses the tcp protocol. The
165 return values and parameters are the same as described for the
166 ping() method. This subroutine is obsolete and may be removed in a
167 future version of Net::Ping.
170 pingecho() or a ping object with the tcp protocol use alarm() to
171 implement the timeout. So, don't use alarm() in your program while you
172 are using pingecho() or a ping object with the tcp protocol. The udp and
173 icmp protocols do not use alarm() to implement the timeout.
176 There will be less network overhead (and some efficiency in your
177 program) if you specify either the udp or the icmp protocol. The tcp
178 protocol will generate 2.5 times or more traffic for each ping than
179 either udp or icmp. If many hosts are pinged frequently, you may wish to
180 implement a small wait (e.g. 25ms or more) between each ping to avoid
181 flooding your network with packets.
183 The icmp protocol requires that the program be run as root or that it be
184 setuid to root. The other protocols do not require special privileges,
185 but not all network devices implement tcp or udp echo.
187 Local hosts should normally respond to pings within milliseconds.
188 However, on a very congested network it may take up to 3 seconds or
189 longer to receive an echo packet from the remote host. If the timeout is
190 set too low under these conditions, it will appear that the remote host
191 is not reachable (which is almost the truth).
193 Reachability doesn't necessarily mean that the remote host is actually
194 functioning beyond its ability to echo packets. tcp is slightly better
195 at indicating the health of a system than icmp because it uses more of
196 the networking stack to respond.
198 Because of a lack of anything better, this module uses its own routines
199 to pack and unpack ICMP packets. It would be better for a separate
200 module to be written which understands all of the different kinds of
205 bbb@cpan.org (Rob Brown)
208 colinm@cpan.org (Colin McMillen)
211 bronson@trestle.com (Scott Bronson)
214 karrer@bernina.ethz.ch (Andreas Karrer)
215 pmarquess@bfsec.bt.co.uk (Paul Marquess)
217 Original Net::Ping author:
218 mose@ns.ccsn.edu (Russell Mosemann)
221 Copyright (c) 2002, Rob Brown. All rights reserved.
223 Copyright (c) 2001, Colin McMillen. All rights reserved.
225 This program is free software; you may redistribute it and/or modify it
226 under the same terms as Perl itself.