1Net::Ping(3pm)         Perl Programmers Reference Guide         Net::Ping(3pm)
2
3
4

NAME

6       Net::Ping - check a remote host for reachability
7

SYNOPSIS

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

DESCRIPTION

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.  This protocol does not require any special privileges.
111
112   Functions
113       Net::Ping->new([proto, timeout, bytes, device, tos, ttl, family, host,
114       port, bind, gateway, retrans, pingstring, source_verify econnrefused
115       dontfrag IPV6_USE_MIN_MTU IPV6_RECVPATHMTU])
116           Create a new ping object.  All of the parameters are optional and
117           can be passed as hash ref.  All options besides the first 7 must be
118           passed as hash ref.
119
120           "proto" specifies the protocol to use when doing a ping.  The
121           current choices are "tcp", "udp", "icmp", "icmpv6", "stream",
122           "syn", or "external".  The default is "tcp".
123
124           If a "timeout" in seconds is provided, it is used when a timeout is
125           not given to the ping() method (below).  The timeout must be
126           greater than 0 and the default, if not specified, is 5 seconds.
127
128           If the number of data bytes ("bytes") is given, that many data
129           bytes are included in the ping packet sent to the remote host. The
130           number of data bytes is ignored if the protocol is "tcp".  The
131           minimum (and default) number of data bytes is 1 if the protocol is
132           "udp" and 0 otherwise.  The maximum number of data bytes that can
133           be specified is 65535, but staying below the MTU (1472 bytes for
134           ICMP) is recommended.  Many small devices cannot deal with
135           fragmented ICMP packets.
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->message_type([$ping_type]);
272           When you are using the "icmp" protocol, this call permit to change
273           the message type to 'echo' or 'timestamp' (only for IPv4, see RFC
274           792).
275
276           Without argument, it returns the currently used icmp protocol
277           message type.  By default, it returns 'echo'.
278
279       $p->open($host);
280           When you are using the "stream" protocol, this call pre-opens the
281           tcp socket.  It's only necessary to do this if you want to provide
282           a different timeout when creating the connection, or remove the
283           overhead of establishing the connection from the first ping.  If
284           you don't call "open()", the connection is automatically opened the
285           first time "ping()" is called.  This call simply does nothing if
286           you are using any protocol other than stream.
287
288           The $host argument can be omitted when specifying the "host" option
289           to new().
290
291       $p->ack( [ $host ] );
292           When using the "syn" protocol, use this method to determine the
293           reachability of the remote host.  This method is meant to be called
294           up to as many times as ping() was called.  Each call returns the
295           host (as passed to ping()) that came back with the TCP ACK.  The
296           order in which the hosts are returned may not necessarily be the
297           same order in which they were SYN queued using the ping() method.
298           If the timeout is reached before the TCP ACK is received, or if the
299           remote host is not listening on the port attempted, then the TCP
300           connection will not be established and ack() will return undef.  In
301           list context, the host, the ack time, the dotted ip string, and the
302           port number will be returned instead of just the host.  If the
303           optional $host argument is specified, the return value will be
304           pertaining to that host only.  This call simply does nothing if you
305           are using any protocol other than "syn".
306
307           When "new" had a host option, this host will be used.  Without
308           $host argument, all hosts are scanned.
309
310       $p->nack( $failed_ack_host );
311           The reason that "host $failed_ack_host" did not receive a valid
312           ACK.  Useful to find out why when "ack($fail_ack_host)" returns a
313           false value.
314
315       $p->ack_unfork($host)
316           The variant called by "ack" with the "syn" protocol and
317           $syn_forking enabled.
318
319       $p->ping_icmp([$host, $timeout, $family])
320           The "ping" method used with the icmp protocol.
321
322       $p->ping_icmpv6([$host, $timeout, $family]) NYI
323           The "ping" method used with the icmpv6 protocol.
324
325       $p->ping_stream([$host, $timeout, $family])
326           The "ping" method used with the stream protocol.
327
328           Perform a stream ping.  If the tcp connection isn't already open,
329           it opens it.  It then sends some data and waits for a reply.  It
330           leaves the stream open on exit.
331
332       $p->ping_syn([$host, $ip, $start_time, $stop_time])
333           The "ping" method used with the syn protocol.  Sends a TCP SYN
334           packet to host specified.
335
336       $p->ping_syn_fork([$host, $timeout, $family])
337           The "ping" method used with the forking syn protocol.
338
339       $p->ping_tcp([$host, $timeout, $family])
340           The "ping" method used with the tcp protocol.
341
342       $p->ping_udp([$host, $timeout, $family])
343           The "ping" method used with the udp protocol.
344
345           Perform a udp echo ping.  Construct a message of at least the one-
346           byte sequence number and any additional data bytes.  Send the
347           message out and wait for a message to come back.  If we get a
348           message, make sure all of its parts match.  If they do, we are
349           done.  Otherwise go back and wait for the message until we run out
350           of time.  Return the result of our efforts.
351
352       $p->ping_external([$host, $timeout, $family])
353           The "ping" method used with the external protocol.  Uses
354           Net::Ping::External to do an external ping.
355
356       $p->tcp_connect([$ip, $timeout])
357           Initiates a TCP connection, for a tcp ping.
358
359       $p->tcp_echo([$ip, $timeout, $pingstring])
360           Performs a TCP echo.  It writes the given string to the socket and
361           then reads it back.  It returns 1 on success, 0 on failure.
362
363       $p->close();
364           Close the network connection for this ping object.  The network
365           connection is also closed by "undef $p".  The network connection is
366           automatically closed if the ping object goes out of scope (e.g. $p
367           is local to a subroutine and you leave the subroutine).
368
369       $p->port_number([$port_number])
370           When called with a port number, the port number used to ping is set
371           to $port_number rather than using the echo port.  It also has the
372           effect of calling "$p->service_check(1)" causing a ping to return a
373           successful response only if that specific port is accessible.  This
374           function returns the value of the port that "ping" will connect to.
375
376       $p->mselect
377           A "select()" wrapper that compensates for platform peculiarities.
378
379       $p->ntop
380           Platform abstraction over "inet_ntop()"
381
382       $p->checksum($msg)
383           Do a checksum on the message.  Basically sum all of the short words
384           and fold the high order bits into the low order bits.
385
386       $p->icmp_result
387           Returns a list of addr, type, subcode.
388
389       pingecho($host [, $timeout]);
390           To provide backward compatibility with the previous version of
391           Net::Ping, a "pingecho()" subroutine is available with the same
392           functionality as before.  "pingecho()" uses the tcp protocol.  The
393           return values and parameters are the same as described for the
394           "ping" method.  This subroutine is obsolete and may be removed in a
395           future version of Net::Ping.
396
397       wakeonlan($mac, [$host, [$port]])
398           Emit the popular wake-on-lan magic udp packet to wake up a local
399           device.  See also Net::Wake, but this has the mac address as 1st
400           arg.  $host should be the local gateway. Without it will broadcast.
401
402           Default host: '255.255.255.255' Default port: 9
403
404             perl -MNet::Ping=wakeonlan -e'wakeonlan "e0:69:95:35:68:d2"'
405

NOTES

407       There will be less network overhead (and some efficiency in your
408       program) if you specify either the udp or the icmp protocol.  The tcp
409       protocol will generate 2.5 times or more traffic for each ping than
410       either udp or icmp.  If many hosts are pinged frequently, you may wish
411       to implement a small wait (e.g. 25ms or more) between each ping to
412       avoid flooding your network with packets.
413
414       The icmp and icmpv6 protocols requires that the program be run as root
415       or that it be setuid to root.  The other protocols do not require
416       special privileges, but not all network devices implement tcp or udp
417       echo.
418
419       Local hosts should normally respond to pings within milliseconds.
420       However, on a very congested network it may take up to 3 seconds or
421       longer to receive an echo packet from the remote host.  If the timeout
422       is set too low under these conditions, it will appear that the remote
423       host is not reachable (which is almost the truth).
424
425       Reachability doesn't necessarily mean that the remote host is actually
426       functioning beyond its ability to echo packets.  tcp is slightly better
427       at indicating the health of a system than icmp because it uses more of
428       the networking stack to respond.
429
430       Because of a lack of anything better, this module uses its own routines
431       to pack and unpack ICMP packets.  It would be better for a separate
432       module to be written which understands all of the different kinds of
433       ICMP packets.
434

INSTALL

436       The latest source tree is available via git:
437
438         git clone https://github.com/rurban/Net-Ping.git
439         cd Net-Ping
440
441       The tarball can be created as follows:
442
443         perl Makefile.PL ; make ; make dist
444
445       The latest Net::Ping releases are included in cperl and perl5.
446

BUGS

448       For a list of known issues, visit:
449
450       <https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Ping> and
451       <https://github.com/rurban/Net-Ping/issues>
452
453       To report a new bug, visit:
454
455       <https://github.com/rurban/Net-Ping/issues>
456

AUTHORS

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) 2017-2018, Reini Urban.  All rights reserved.
484
485       Copyright (c) 2016, cPanel Inc.  All rights reserved.
486
487       Copyright (c) 2012, Steve Peters.  All rights reserved.
488
489       Copyright (c) 2002-2003, Rob Brown.  All rights reserved.
490
491       Copyright (c) 2001, Colin McMillen.  All rights reserved.
492
493       This program is free software; you may redistribute it and/or modify it
494       under the same terms as Perl itself.
495
496
497
498perl v5.32.1                      2021-03-31                    Net::Ping(3pm)
Impressum