Commit | Line | Data |
505f3f16 |
1 | NAME |
2 | Net::Ping - check a remote host for reachability |
3 | |
4 | $Id: Ping.pm,v 1.13 2001/12/07 02:18:44 rob Exp $ |
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"); |
14 | foreach $host (@host_array) |
15 | { |
16 | print "$host is "; |
17 | print "NOT " unless $p->ping($host, 2); |
18 | print "reachable.\n"; |
19 | sleep(1); |
20 | } |
21 | $p->close(); |
22 | |
23 | $p = Net::Ping->new("tcp", 2); |
24 | # Try connecting to the www port instead of the echo port |
25 | $p->{port_num} = getservbyname("http", "tcp"); |
26 | while ($stop_time > time()) |
27 | { |
28 | print "$host not reachable ", scalar(localtime()), "\n" |
29 | unless $p->ping($host); |
30 | sleep(300); |
31 | } |
32 | undef($p); |
33 | |
34 | # For backward compatibility |
35 | print "$host is alive.\n" if pingecho($host); |
36 | |
37 | DESCRIPTION |
38 | This module contains methods to test the reachability of remote hosts on |
39 | a network. A ping object is first created with optional parameters, a |
40 | variable number of hosts may be pinged multiple times and then the |
41 | connection is closed. |
42 | |
43 | You may choose one of four different protocols to use for the ping. The |
44 | "udp" protocol is the default. Note that a live remote host may still |
45 | fail to be pingable by one or more of these protocols. For example, |
46 | www.microsoft.com is generally alive but not pingable. |
47 | |
48 | With the "tcp" protocol the ping() method attempts to establish a |
49 | connection to the remote host's echo port. If the connection is |
50 | successfully established, the remote host is considered reachable. No |
51 | data is actually echoed. This protocol does not require any special |
52 | privileges but has higher overhead than the other two protocols. |
53 | |
54 | Specifying the "udp" protocol causes the ping() method to send a udp |
55 | packet to the remote host's echo port. If the echoed packet is received |
56 | from the remote host and the received packet contains the same data as |
57 | the packet that was sent, the remote host is considered reachable. This |
58 | protocol does not require any special privileges. It should be borne in |
59 | mind that, for a udp ping, a host will be reported as unreachable if it |
60 | is not running the appropriate echo service. For Unix-like systems see |
61 | the inetd(8) manpage for more information. |
62 | |
63 | If the "icmp" protocol is specified, the ping() method sends an icmp |
64 | echo message to the remote host, which is what the UNIX ping program |
65 | does. If the echoed message is received from the remote host and the |
66 | echoed information is correct, the remote host is considered reachable. |
67 | Specifying the "icmp" protocol requires that the program be run as root |
68 | or that the program be setuid to root. |
69 | |
70 | If the "external" protocol is specified, the ping() method attempts to |
71 | use the `Net::Ping::External' module to ping the remote host. |
72 | `Net::Ping::External' interfaces with your system's default `ping' |
73 | utility to perform the ping, and generally produces relatively accurate |
74 | results. If `Net::Ping::External' if not installed on your system, |
75 | specifying the "external" protocol will result in an error. |
76 | |
77 | Functions |
78 | |
79 | Net::Ping->new([$proto [, $def_timeout [, $bytes]]]); |
80 | Create a new ping object. All of the parameters are optional. $proto |
81 | specifies the protocol to use when doing a ping. The current choices |
82 | are "tcp", "udp" or "icmp". The default is "udp". |
83 | |
84 | If a default timeout ($def_timeout) in seconds is provided, it is |
85 | used when a timeout is not given to the ping() method (below). The |
86 | timeout must be greater than 0 and the default, if not specified, is |
87 | 5 seconds. |
88 | |
89 | If the number of data bytes ($bytes) is given, that many data bytes |
90 | are included in the ping packet sent to the remote host. The number |
91 | of data bytes is ignored if the protocol is "tcp". The minimum (and |
92 | default) number of data bytes is 1 if the protocol is "udp" and 0 |
93 | otherwise. The maximum number of data bytes that can be specified is |
94 | 1024. |
95 | |
96 | $p->ping($host [, $timeout]); |
97 | Ping the remote host and wait for a response. $host can be either |
98 | the hostname or the IP number of the remote host. The optional |
99 | timeout must be greater than 0 seconds and defaults to whatever was |
100 | specified when the ping object was created. If the hostname cannot |
101 | be found or there is a problem with the IP number, undef is |
102 | returned. Otherwise, 1 is returned if the host is reachable and 0 if |
103 | it is not. For all practical purposes, undef and 0 and can be |
104 | treated as the same case. |
105 | |
106 | $p->open($host); |
107 | When you are using the stream protocol, this call pre-opens the tcp |
108 | socket. It's only necessary to do this if you want to provide a |
109 | different timeout when creating the connection, or remove the |
110 | overhead of establishing the connection from the first ping. If you |
111 | don't call `open()', the connection is automatically opened the |
112 | first time `ping()' is called. This call simply does nothing if you |
113 | are using any protocol other than stream. |
114 | |
115 | $p->open($host); |
116 | When you are using the stream protocol, this call pre-opens the tcp |
117 | socket. It's only necessary to do this if you want to provide a |
118 | different timeout when creating the connection, or remove the |
119 | overhead of establishing the connection from the first ping. If you |
120 | don't call `open()', the connection is automatically opened the |
121 | first time `ping()' is called. This call simply does nothing if you |
122 | are using any protocol other than stream. |
123 | |
124 | $p->close(); |
125 | Close the network connection for this ping object. The network |
126 | connection is also closed by "undef $p". The network connection is |
127 | automatically closed if the ping object goes out of scope (e.g. $p |
128 | is local to a subroutine and you leave the subroutine). |
129 | |
130 | pingecho($host [, $timeout]); |
131 | To provide backward compatibility with the previous version of |
132 | Net::Ping, a pingecho() subroutine is available with the same |
133 | functionality as before. pingecho() uses the tcp protocol. The |
134 | return values and parameters are the same as described for the |
135 | ping() method. This subroutine is obsolete and may be removed in a |
136 | future version of Net::Ping. |
137 | |
138 | WARNING |
139 | pingecho() or a ping object with the tcp protocol use alarm() to |
140 | implement the timeout. So, don't use alarm() in your program while you |
141 | are using pingecho() or a ping object with the tcp protocol. The udp and |
142 | icmp protocols do not use alarm() to implement the timeout. |
143 | |
144 | NOTES |
145 | There will be less network overhead (and some efficiency in your |
146 | program) if you specify either the udp or the icmp protocol. The tcp |
147 | protocol will generate 2.5 times or more traffic for each ping than |
148 | either udp or icmp. If many hosts are pinged frequently, you may wish to |
149 | implement a small wait (e.g. 25ms or more) between each ping to avoid |
150 | flooding your network with packets. |
151 | |
152 | The icmp protocol requires that the program be run as root or that it be |
153 | setuid to root. The other protocols do not require special privileges, |
154 | but not all network devices implement tcp or udp echo. |
155 | |
156 | Local hosts should normally respond to pings within milliseconds. |
157 | However, on a very congested network it may take up to 3 seconds or |
158 | longer to receive an echo packet from the remote host. If the timeout is |
159 | set too low under these conditions, it will appear that the remote host |
160 | is not reachable (which is almost the truth). |
161 | |
162 | Reachability doesn't necessarily mean that the remote host is actually |
163 | functioning beyond its ability to echo packets. tcp is slightly better |
164 | at indicating the health of a system than icmp because it uses more of |
165 | the networking stack to respond. |
166 | |
167 | Because of a lack of anything better, this module uses its own routines |
168 | to pack and unpack ICMP packets. It would be better for a separate |
169 | module to be written which understands all of the different kinds of |
170 | ICMP packets. |
171 | |
172 | AUTHOR(S) |
173 | Current maintainer Net::Ping base code: |
174 | colinm@cpan.org (Colin McMillen) |
175 | |
176 | Stream protocol: |
177 | bronson@trestle.com (Scott Bronson) |
178 | |
179 | Original pingecho(): |
180 | karrer@bernina.ethz.ch (Andreas Karrer) |
181 | pmarquess@bfsec.bt.co.uk (Paul Marquess) |
182 | |
183 | Original Net::Ping author: |
184 | mose@ns.ccsn.edu (Russell Mosemann) |
185 | |
186 | Compatibility porting: |
187 | bbb@cpan.org (Rob Brown) |
188 | |
189 | COPYRIGHT |
190 | Copyright (c) 2001, Colin McMillen. All rights reserved. Copyright (c) |
191 | 2001, Rob Brown. All rights reserved. |
192 | |
193 | This program is free software; you may redistribute it and/or modify it |
194 | under the same terms as Perl itself. |
195 | |