Commit | Line | Data |
505f3f16 |
1 | NAME |
2 | Net::Ping - check a remote host for reachability |
3 | |
15d96390 |
4 | $Id: Ping.pm,v 1.1 2002/06/04 00:41:52 rob Exp $ |
505f3f16 |
5 | |
6 | SYNOPSIS |
7 | use Net::Ping; |
8 | |
9 | $p = Net::Ping->new(); |
10 | print "$host is alive.\n" if $p->ping($host); |
11 | $p->close(); |
12 | |
13 | $p = Net::Ping->new("icmp"); |
ddbbf559 |
14 | $p->bind($my_addr); # Specify source interface of pings |
505f3f16 |
15 | foreach $host (@host_array) |
16 | { |
17 | print "$host is "; |
18 | print "NOT " unless $p->ping($host, 2); |
19 | print "reachable.\n"; |
20 | sleep(1); |
21 | } |
22 | $p->close(); |
23 | |
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()) |
28 | { |
29 | print "$host not reachable ", scalar(localtime()), "\n" |
30 | unless $p->ping($host); |
31 | sleep(300); |
32 | } |
33 | undef($p); |
34 | |
e82f584b |
35 | # High precision syntax (requires Time::HiRes) |
36 | $p = Net::Ping->new(); |
37 | $p->hires(); |
38 | ($ret, $duration, $ip) = $p->ping($host, 5.5); |
39 | printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration) |
40 | if $ret; |
41 | $p->close(); |
42 | |
505f3f16 |
43 | # For backward compatibility |
44 | print "$host is alive.\n" if pingecho($host); |
45 | |
46 | DESCRIPTION |
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 |
50 | connection is closed. |
51 | |
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. |
56 | |
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. |
62 | |
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. |
71 | |
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. |
78 | |
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. |
85 | |
86 | Functions |
87 | |
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". |
92 | |
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 |
96 | 5 seconds. |
97 | |
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 |
103 | 1024. |
104 | |
15d96390 |
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. |
110 | |
111 | This is enabled by default. |
112 | |
e82f584b |
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(). |
116 | |
15d96390 |
117 | This is disabled by default. |
118 | |
ddbbf559 |
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 |
123 | "192.168.1.1". |
124 | |
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 |
130 | that object. |
131 | |
505f3f16 |
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 |
e82f584b |
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. |
505f3f16 |
145 | |
146 | $p->open($host); |
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. |
154 | |
155 | $p->close(); |
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). |
160 | |
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. |
168 | |
169 | WARNING |
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. |
174 | |
175 | NOTES |
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. |
182 | |
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. |
186 | |
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). |
192 | |
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. |
197 | |
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 |
201 | ICMP packets. |
202 | |
ddbbf559 |
203 | AUTHORS |
e82f584b |
204 | Current maintainer: |
ddbbf559 |
205 | bbb@cpan.org (Rob Brown) |
505f3f16 |
206 | |
e82f584b |
207 | External protocol: |
208 | colinm@cpan.org (Colin McMillen) |
209 | |
505f3f16 |
210 | Stream protocol: |
211 | bronson@trestle.com (Scott Bronson) |
212 | |
213 | Original pingecho(): |
214 | karrer@bernina.ethz.ch (Andreas Karrer) |
215 | pmarquess@bfsec.bt.co.uk (Paul Marquess) |
216 | |
217 | Original Net::Ping author: |
218 | mose@ns.ccsn.edu (Russell Mosemann) |
219 | |
505f3f16 |
220 | COPYRIGHT |
e82f584b |
221 | Copyright (c) 2002, Rob Brown. All rights reserved. |
ddbbf559 |
222 | |
e82f584b |
223 | Copyright (c) 2001, Colin McMillen. All rights reserved. |
505f3f16 |
224 | |
225 | This program is free software; you may redistribute it and/or modify it |
226 | under the same terms as Perl itself. |
227 | |