1Net::Ping(3pm) Perl Programmers Reference Guide Net::Ping(3pm)
2
3
4
6 Net::Ping - check a remote host for reachability
7
9 use Net::Ping;
10
11 $p = Net::Ping->new();
12 print "$host is alive.\n" if $p->ping($host);
13 $p->close();
14
15 $p = Net::Ping->new("icmp");
16 $p->bind($my_addr); # Specify source interface of pings
17 foreach $host (@host_array)
18 {
19 print "$host is ";
20 print "NOT " unless $p->ping($host, 2);
21 print "reachable.\n";
22 sleep(1);
23 }
24 $p->close();
25
26 $p = Net::Ping->new("tcp", 2);
27 # Try connecting to the www port instead of the echo port
28 $p->port_number(scalar(getservbyname("http", "tcp")));
29 while ($stop_time > time())
30 {
31 print "$host not reachable ", scalar(localtime()), "\n"
32 unless $p->ping($host);
33 sleep(300);
34 }
35 undef($p);
36
37 # Like tcp protocol, but with many hosts
38 $p = Net::Ping->new("syn");
39 $p->port_number(getservbyname("http", "tcp"));
40 foreach $host (@host_array) {
41 $p->ping($host);
42 }
43 while (($host,$rtt,$ip) = $p->ack) {
44 print "HOST: $host [$ip] ACKed in $rtt seconds.\n";
45 }
46
47 # High precision syntax (requires Time::HiRes)
48 $p = Net::Ping->new();
49 $p->hires();
50 ($ret, $duration, $ip) = $p->ping($host, 5.5);
51 printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n",
52 1000 * $duration)
53 if $ret;
54 $p->close();
55
56 # For backward compatibility
57 print "$host is alive.\n" if pingecho($host);
58
60 This module contains methods to test the reachability of remote hosts
61 on a network. A ping object is first created with optional parameters,
62 a variable number of hosts may be pinged multiple times and then the
63 connection is closed.
64
65 You may choose one of six different protocols to use for the ping. The
66 "tcp" protocol is the default. Note that a live remote host may still
67 fail to be pingable by one or more of these protocols. For example,
68 www.microsoft.com is generally alive but not "icmp" pingable.
69
70 With the "tcp" protocol the ping() method attempts to establish a
71 connection to the remote host's echo port. If the connection is
72 successfully established, the remote host is considered reachable. No
73 data is actually echoed. This protocol does not require any special
74 privileges but has higher overhead than the "udp" and "icmp" protocols.
75
76 Specifying the "udp" protocol causes the ping() method to send a udp
77 packet to the remote host's echo port. If the echoed packet is
78 received from the remote host and the received packet contains the same
79 data as the packet that was sent, the remote host is considered
80 reachable. This protocol does not require any special privileges. It
81 should be borne in mind that, for a udp ping, a host will be reported
82 as unreachable if it is not running the appropriate echo service. For
83 Unix-like systems see inetd(8) for more information.
84
85 If the "icmp" protocol is specified, the ping() method sends an icmp
86 echo message to the remote host, which is what the UNIX ping program
87 does. If the echoed message is received from the remote host and the
88 echoed information is correct, the remote host is considered reachable.
89 Specifying the "icmp" protocol requires that the program be run as root
90 or that the program be setuid to root.
91
92 If the "external" protocol is specified, the ping() method attempts to
93 use the "Net::Ping::External" module to ping the remote host.
94 "Net::Ping::External" interfaces with your system's default "ping"
95 utility to perform the ping, and generally produces relatively accurate
96 results. If "Net::Ping::External" if not installed on your system,
97 specifying the "external" protocol will result in an error.
98
99 If the "syn" protocol is specified, the ping() method will only send a
100 TCP SYN packet to the remote host then immediately return. If the syn
101 packet was sent successfully, it will return a true value, otherwise it
102 will return false. NOTE: Unlike the other protocols, the return value
103 does NOT determine if the remote host is alive or not since the full
104 TCP three-way handshake may not have completed yet. The remote host is
105 only considered reachable if it receives a TCP ACK within the timeout
106 specified. To begin waiting for the ACK packets, use the ack() method
107 as explained below. Use the "syn" protocol instead the "tcp" protocol
108 to determine reachability of multiple destinations simultaneously by
109 sending parallel TCP SYN packets. It will not block while testing each
110 remote host. demo/fping is provided in this distribution to
111 demonstrate the "syn" protocol as an example. This protocol does not
112 require any special privileges.
113
114 Functions
115 Net::Ping->new([proto, timeout, bytes, device, tos, ttl, family, host,
116 port, bind, gateway, retrans, pingstring, source_verify econnrefused
117 dontfrag IPV6_USE_MIN_MTU IPV6_RECVPATHMTU])
118 Create a new ping object. All of the parameters are optional and
119 can be passed as hash ref. All options besides the first 7 must be
120 passed as hash ref.
121
122 "proto" specifies the protocol to use when doing a ping. The
123 current choices are "tcp", "udp", "icmp", "icmpv6", "stream",
124 "syn", or "external". The default is "tcp".
125
126 If a "timeout" in seconds is provided, it is used when a timeout is
127 not given to the ping() method (below). The timeout must be
128 greater than 0 and the default, if not specified, is 5 seconds.
129
130 If the number of data bytes ("bytes") is given, that many data
131 bytes are included in the ping packet sent to the remote host. The
132 number of data bytes is ignored if the protocol is "tcp". The
133 minimum (and default) number of data bytes is 1 if the protocol is
134 "udp" and 0 otherwise. The maximum number of data bytes that can
135 be specified is 1024.
136
137 If "device" is given, this device is used to bind the source
138 endpoint before sending the ping packet. I believe this only works
139 with superuser privileges and with udp and icmp protocols at this
140 time.
141
142 If <tos> is given, this ToS is configured into the socket.
143
144 For icmp, "ttl" can be specified to set the TTL of the outgoing
145 packet.
146
147 Valid "family" values for IPv4:
148
149 4, v4, ip4, ipv4, AF_INET (constant)
150
151 Valid "family" values for IPv6:
152
153 6, v6, ip6, ipv6, AF_INET6 (constant)
154
155 The "host" argument implicitly specifies the family if the family
156 argument is not given.
157
158 The "port" argument is only valid for a udp, tcp or stream ping,
159 and will not do what you think it does. ping returns true when we
160 get a "Connection refused"! The default is the echo port.
161
162 The "bind" argument specifies the local_addr to bind to. By
163 specifying a bind argument you don't need the bind method.
164
165 The "gateway" argument is only valid for IPv6, and requires a IPv6
166 address.
167
168 The "retrans" argument the exponential backoff rate, default 1.2.
169 It matches the $def_factor global.
170
171 The "dontfrag" argument sets the IP_DONTFRAG bit, but note that
172 IP_DONTFRAG is not yet defined by Socket, and not available on many
173 systems. Then it is ignored. On linux it also sets IP_MTU_DISCOVER
174 to IP_PMTUDISC_DO but need we don't chunk oversized packets. You
175 need to set $data_size manually.
176
177 $p->ping($host [, $timeout [, $family]]);
178 Ping the remote host and wait for a response. $host can be either
179 the hostname or the IP number of the remote host. The optional
180 timeout must be greater than 0 seconds and defaults to whatever was
181 specified when the ping object was created. Returns a success
182 flag. If the hostname cannot be found or there is a problem with
183 the IP number, the success flag returned will be undef. Otherwise,
184 the success flag will be 1 if the host is reachable and 0 if it is
185 not. For most practical purposes, undef and 0 and can be treated
186 as the same case. In array context, the elapsed time as well as
187 the string form of the ip the host resolved to are also returned.
188 The elapsed time value will be a float, as returned by the
189 Time::HiRes::time() function, if hires() has been previously
190 called, otherwise it is returned as an integer.
191
192 $p->source_verify( { 0 | 1 } );
193 Allows source endpoint verification to be enabled or disabled.
194 This is useful for those remote destinations with multiples
195 interfaces where the response may not originate from the same
196 endpoint that the original destination endpoint was sent to. This
197 only affects udp and icmp protocol pings.
198
199 This is enabled by default.
200
201 $p->service_check( { 0 | 1 } );
202 Set whether or not the connect behavior should enforce remote
203 service availability as well as reachability. Normally, if the
204 remote server reported ECONNREFUSED, it must have been reachable
205 because of the status packet that it reported. With this option
206 enabled, the full three-way tcp handshake must have been
207 established successfully before it will claim it is reachable.
208 NOTE: It still does nothing more than connect and disconnect. It
209 does not speak any protocol (i.e., HTTP or FTP) to ensure the
210 remote server is sane in any way. The remote server CPU could be
211 grinding to a halt and unresponsive to any clients connecting, but
212 if the kernel throws the ACK packet, it is considered alive anyway.
213 To really determine if the server is responding well would be
214 application specific and is beyond the scope of Net::Ping. For udp
215 protocol, enabling this option demands that the remote server
216 replies with the same udp data that it was sent as defined by the
217 udp echo service.
218
219 This affects the "udp", "tcp", and "syn" protocols.
220
221 This is disabled by default.
222
223 $p->tcp_service_check( { 0 | 1 } );
224 Deprecated method, but does the same as service_check() method.
225
226 $p->hires( { 0 | 1 } );
227 With 1 causes this module to use Time::HiRes module, allowing
228 milliseconds to be returned by subsequent calls to ping().
229
230 $p->time
231 The current time, hires or not.
232
233 $p->socket_blocking_mode( $fh, $mode );
234 Sets or clears the O_NONBLOCK flag on a file handle.
235
236 $p->IPV6_USE_MIN_MTU
237 With argument sets the option. Without returns the option value.
238
239 $p->IPV6_RECVPATHMTU
240 Notify an according IPv6 MTU.
241
242 With argument sets the option. Without returns the option value.
243
244 $p->IPV6_HOPLIMIT
245 With argument sets the option. Without returns the option value.
246
247 $p->IPV6_REACHCONF NYI
248 Sets ipv6 reachability IPV6_REACHCONF was removed in RFC3542. ping6
249 -R supports it. IPV6_REACHCONF requires root/admin permissions.
250
251 With argument sets the option. Without returns the option value.
252
253 Not yet implemented.
254
255 $p->bind($local_addr);
256 Sets the source address from which pings will be sent. This must
257 be the address of one of the interfaces on the local host.
258 $local_addr may be specified as a hostname or as a text IP address
259 such as "192.168.1.1".
260
261 If the protocol is set to "tcp", this method may be called any
262 number of times, and each call to the ping() method (below) will
263 use the most recent $local_addr. If the protocol is "icmp" or
264 "udp", then bind() must be called at most once per object, and (if
265 it is called at all) must be called before the first call to ping()
266 for that object.
267
268 The bind() call can be omitted when specifying the "bind" option to
269 new().
270
271 $p->open($host);
272 When you are using the "stream" protocol, this call pre-opens the
273 tcp socket. It's only necessary to do this if you want to provide
274 a different timeout when creating the connection, or remove the
275 overhead of establishing the connection from the first ping. If
276 you don't call "open()", the connection is automatically opened the
277 first time "ping()" is called. This call simply does nothing if
278 you are using any protocol other than stream.
279
280 The $host argument can be omitted when specifying the "host" option
281 to new().
282
283 $p->ack( [ $host ] );
284 When using the "syn" protocol, use this method to determine the
285 reachability of the remote host. This method is meant to be called
286 up to as many times as ping() was called. Each call returns the
287 host (as passed to ping()) that came back with the TCP ACK. The
288 order in which the hosts are returned may not necessarily be the
289 same order in which they were SYN queued using the ping() method.
290 If the timeout is reached before the TCP ACK is received, or if the
291 remote host is not listening on the port attempted, then the TCP
292 connection will not be established and ack() will return undef. In
293 list context, the host, the ack time, the dotted ip string, and the
294 port number will be returned instead of just the host. If the
295 optional $host argument is specified, the return value will be
296 pertaining to that host only. This call simply does nothing if you
297 are using any protocol other than syn.
298
299 When new() had a host option, this host will be used. Without host
300 argument, all hosts are scanned.
301
302 $p->nack( $failed_ack_host );
303 The reason that host $failed_ack_host did not receive a valid ACK.
304 Useful to find out why when ack( $fail_ack_host ) returns a false
305 value.
306
307 $p->ack_unfork($host)
308 The variant called by ack() with the syn protocol and $syn_forking
309 enabled.
310
311 $p->ping_icmp([$host, $timeout, $family])
312 The ping() method used with the icmp protocol.
313
314 $p->ping_icmpv6([$host, $timeout, $family]) NYI
315 The ping() method used with the icmpv6 protocol.
316
317 $p->ping_stream([$host, $timeout, $family])
318 The ping() method used with the stream protocol.
319
320 Perform a stream ping. If the tcp connection isn't already open,
321 it opens it. It then sends some data and waits for a reply. It
322 leaves the stream open on exit.
323
324 $p->ping_syn([$host, $ip, $start_time, $stop_time])
325 The ping() method used with the syn protocol. Sends a TCP SYN
326 packet to host specified.
327
328 $p->ping_syn_fork([$host, $timeout, $family])
329 The ping() method used with the forking syn protocol.
330
331 $p->ping_tcp([$host, $timeout, $family])
332 The ping() method used with the tcp protocol.
333
334 $p->ping_udp([$host, $timeout, $family])
335 The ping() method used with the udp protocol.
336
337 Perform a udp echo ping. Construct a message of at least the one-
338 byte sequence number and any additional data bytes. Send the
339 message out and wait for a message to come back. If we get a
340 message, make sure all of its parts match. If they do, we are
341 done. Otherwise go back and wait for the message until we run out
342 of time. Return the result of our efforts.
343
344 $p->ping_external([$host, $timeout, $family])
345 The ping() method used with the external protocol. Uses
346 Net::Ping::External to do an external ping.
347
348 $p->tcp_connect([$ip, $timeout])
349 Initiates a TCP connection, for a tcp ping.
350
351 $p->tcp_echo([$ip, $timeout, $pingstring])
352 Performs a TCP echo. It writes the given string to the socket and
353 then reads it back. It returns 1 on success, 0 on failure.
354
355 $p->close();
356 Close the network connection for this ping object. The network
357 connection is also closed by "undef $p". The network connection is
358 automatically closed if the ping object goes out of scope (e.g. $p
359 is local to a subroutine and you leave the subroutine).
360
361 $p->port_number([$port_number])
362 When called with a port number, the port number used to ping is set
363 to $port_number rather than using the echo port. It also has the
364 effect of calling "$p->service_check(1)" causing a ping to return a
365 successful response only if that specific port is accessible. This
366 function returns the value of the port that "ping()" will connect
367 to.
368
369 $p->mselect
370 A select() wrapper that compensates for platform peculiarities.
371
372 $p->ntop
373 Platform abstraction over inet_ntop()
374
375 $p->checksum($msg)
376 Do a checksum on the message. Basically sum all of the short words
377 and fold the high order bits into the low order bits.
378
379 $p->icmp_result
380 Returns a list of addr, type, subcode.
381
382 pingecho($host [, $timeout]);
383 To provide backward compatibility with the previous version of
384 Net::Ping, a pingecho() subroutine is available with the same
385 functionality as before. pingecho() uses the tcp protocol. The
386 return values and parameters are the same as described for the
387 ping() method. This subroutine is obsolete and may be removed in a
388 future version of Net::Ping.
389
390 wakeonlan($mac, [$host, [$port]])
391 Emit the popular wake-on-lan magic udp packet to wake up a local
392 device. See also Net::Wake, but this has the mac address as 1st
393 arg. $host should be the local gateway. Without it will broadcast.
394
395 Default host: '255.255.255.255' Default port: 9
396
397 perl -MNet::Ping=wakeonlan -e'wakeonlan "e0:69:95:35:68:d2"'
398
400 There will be less network overhead (and some efficiency in your
401 program) if you specify either the udp or the icmp protocol. The tcp
402 protocol will generate 2.5 times or more traffic for each ping than
403 either udp or icmp. If many hosts are pinged frequently, you may wish
404 to implement a small wait (e.g. 25ms or more) between each ping to
405 avoid flooding your network with packets.
406
407 The icmp and icmpv6 protocols requires that the program be run as root
408 or that it be setuid to root. The other protocols do not require
409 special privileges, but not all network devices implement tcp or udp
410 echo.
411
412 Local hosts should normally respond to pings within milliseconds.
413 However, on a very congested network it may take up to 3 seconds or
414 longer to receive an echo packet from the remote host. If the timeout
415 is set too low under these conditions, it will appear that the remote
416 host is not reachable (which is almost the truth).
417
418 Reachability doesn't necessarily mean that the remote host is actually
419 functioning beyond its ability to echo packets. tcp is slightly better
420 at indicating the health of a system than icmp because it uses more of
421 the networking stack to respond.
422
423 Because of a lack of anything better, this module uses its own routines
424 to pack and unpack ICMP packets. It would be better for a separate
425 module to be written which understands all of the different kinds of
426 ICMP packets.
427
429 The latest source tree is available via git:
430
431 git clone https://github.com/rurban/net-ping.git Net-Ping
432 cd Net-Ping
433
434 The tarball can be created as follows:
435
436 perl Makefile.PL ; make ; make dist
437
438 The latest Net::Ping releases are included in cperl and perl5.
439
441 For a list of known issues, visit:
442
443 <https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Ping>
444
445 To report a new bug, visit:
446
447 <https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Ping> (stale)
448
449 or call:
450
451 perlbug
452
453 resp.:
454
455 cperlbug
456
458 Current maintainers:
459 perl11 (for cperl, with IPv6 support and more)
460 p5p (for perl5)
461
462 Previous maintainers:
463 bbb@cpan.org (Rob Brown)
464 Steve Peters
465
466 External protocol:
467 colinm@cpan.org (Colin McMillen)
468
469 Stream protocol:
470 bronson@trestle.com (Scott Bronson)
471
472 Wake-on-lan:
473 1999-2003 Clinton Wong
474
475 Original pingecho():
476 karrer@bernina.ethz.ch (Andreas Karrer)
477 pmarquess@bfsec.bt.co.uk (Paul Marquess)
478
479 Original Net::Ping author:
480 mose@ns.ccsn.edu (Russell Mosemann)
481
483 Copyright (c) 2016, cPanel Inc. All rights reserved.
484
485 Copyright (c) 2012, Steve Peters. All rights reserved.
486
487 Copyright (c) 2002-2003, Rob Brown. All rights reserved.
488
489 Copyright (c) 2001, Colin McMillen. All rights reserved.
490
491 This program is free software; you may redistribute it and/or modify it
492 under the same terms as Perl itself.
493
494
495
496perl v5.28.2 2018-11-01 Net::Ping(3pm)