Commit | Line | Data |
505f3f16 |
1 | NAME |
2 | Net::Ping - check a remote host for reachability |
3 | |
564e2e78 |
4 | $Id: Ping.pm,v 1.34 2002/05/06 17:37:54 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 | |
e82f584b |
105 | $p->hires( { 0 | 1 } ); |
106 | Causes this module to use Time::HiRes module, allowing milliseconds |
107 | to be returned by subsequent calls to ping(). |
108 | |
ddbbf559 |
109 | $p->bind($local_addr); |
110 | Sets the source address from which pings will be sent. This must be |
111 | the address of one of the interfaces on the local host. $local_addr |
112 | may be specified as a hostname or as a text IP address such as |
113 | "192.168.1.1". |
114 | |
115 | If the protocol is set to "tcp", this method may be called any |
116 | number of times, and each call to the ping() method (below) will use |
117 | the most recent $local_addr. If the protocol is "icmp" or "udp", |
118 | then bind() must be called at most once per object, and (if it is |
119 | called at all) must be called before the first call to ping() for |
120 | that object. |
121 | |
505f3f16 |
122 | $p->ping($host [, $timeout]); |
123 | Ping the remote host and wait for a response. $host can be either |
124 | the hostname or the IP number of the remote host. The optional |
125 | timeout must be greater than 0 seconds and defaults to whatever was |
e82f584b |
126 | specified when the ping object was created. Returns a success flag. |
127 | If the hostname cannot be found or there is a problem with the IP |
128 | number, the success flag returned will be undef. Otherwise, the |
129 | success flag will be 1 if the host is reachable and 0 if it is not. |
130 | For most practical purposes, undef and 0 and can be treated as the |
131 | same case. In array context, the elapsed time is also returned. The |
132 | elapsed time value will be a float, as retuned by the |
133 | Time::HiRes::time() function, if hires() has been previously called, |
134 | otherwise it is returned as an integer. |
505f3f16 |
135 | |
136 | $p->open($host); |
137 | When you are using the stream protocol, this call pre-opens the tcp |
138 | socket. It's only necessary to do this if you want to provide a |
139 | different timeout when creating the connection, or remove the |
140 | overhead of establishing the connection from the first ping. If you |
141 | don't call `open()', the connection is automatically opened the |
142 | first time `ping()' is called. This call simply does nothing if you |
143 | are using any protocol other than stream. |
144 | |
145 | $p->close(); |
146 | Close the network connection for this ping object. The network |
147 | connection is also closed by "undef $p". The network connection is |
148 | automatically closed if the ping object goes out of scope (e.g. $p |
149 | is local to a subroutine and you leave the subroutine). |
150 | |
151 | pingecho($host [, $timeout]); |
152 | To provide backward compatibility with the previous version of |
153 | Net::Ping, a pingecho() subroutine is available with the same |
154 | functionality as before. pingecho() uses the tcp protocol. The |
155 | return values and parameters are the same as described for the |
156 | ping() method. This subroutine is obsolete and may be removed in a |
157 | future version of Net::Ping. |
158 | |
159 | WARNING |
160 | pingecho() or a ping object with the tcp protocol use alarm() to |
161 | implement the timeout. So, don't use alarm() in your program while you |
162 | are using pingecho() or a ping object with the tcp protocol. The udp and |
163 | icmp protocols do not use alarm() to implement the timeout. |
164 | |
165 | NOTES |
166 | There will be less network overhead (and some efficiency in your |
167 | program) if you specify either the udp or the icmp protocol. The tcp |
168 | protocol will generate 2.5 times or more traffic for each ping than |
169 | either udp or icmp. If many hosts are pinged frequently, you may wish to |
170 | implement a small wait (e.g. 25ms or more) between each ping to avoid |
171 | flooding your network with packets. |
172 | |
173 | The icmp protocol requires that the program be run as root or that it be |
174 | setuid to root. The other protocols do not require special privileges, |
175 | but not all network devices implement tcp or udp echo. |
176 | |
177 | Local hosts should normally respond to pings within milliseconds. |
178 | However, on a very congested network it may take up to 3 seconds or |
179 | longer to receive an echo packet from the remote host. If the timeout is |
180 | set too low under these conditions, it will appear that the remote host |
181 | is not reachable (which is almost the truth). |
182 | |
183 | Reachability doesn't necessarily mean that the remote host is actually |
184 | functioning beyond its ability to echo packets. tcp is slightly better |
185 | at indicating the health of a system than icmp because it uses more of |
186 | the networking stack to respond. |
187 | |
188 | Because of a lack of anything better, this module uses its own routines |
189 | to pack and unpack ICMP packets. It would be better for a separate |
190 | module to be written which understands all of the different kinds of |
191 | ICMP packets. |
192 | |
ddbbf559 |
193 | AUTHORS |
e82f584b |
194 | Current maintainer: |
ddbbf559 |
195 | bbb@cpan.org (Rob Brown) |
505f3f16 |
196 | |
e82f584b |
197 | External protocol: |
198 | colinm@cpan.org (Colin McMillen) |
199 | |
505f3f16 |
200 | Stream protocol: |
201 | bronson@trestle.com (Scott Bronson) |
202 | |
203 | Original pingecho(): |
204 | karrer@bernina.ethz.ch (Andreas Karrer) |
205 | pmarquess@bfsec.bt.co.uk (Paul Marquess) |
206 | |
207 | Original Net::Ping author: |
208 | mose@ns.ccsn.edu (Russell Mosemann) |
209 | |
505f3f16 |
210 | COPYRIGHT |
e82f584b |
211 | Copyright (c) 2002, Rob Brown. All rights reserved. |
ddbbf559 |
212 | |
e82f584b |
213 | Copyright (c) 2001, Colin McMillen. All rights reserved. |
505f3f16 |
214 | |
215 | This program is free software; you may redistribute it and/or modify it |
216 | under the same terms as Perl itself. |
217 | |