Win32::PingICMP - ICMP Ping support for Win32 based on ICMP.DLL
use Win32::PingICMP; use Data::Dumper; my $p = Win32::PingICMP->new(); if ($p->ping(@ARGV)) { print "Ping took ".$p->details->{roundtriptime}."\n"; } else { print "Ping unsuccessful: ".$p->details->{status}."\n"; } print Data::Dumper->Dump([$p->details()]); $p->ping_async(@ARGV); until ($p->wait(0)) { Win32::Sleep(10); print "Waiting\n"; } if ($p->details()->{status} eq 'IP_SUCCESS') { print "Ping took ".$p->details()->{roundtriptime}."\n"; } else { print "Ping unsuccessful: ".$p->details()->{status}."\n"; } print Data::Dumper->Dump([$p->details()]);
Win32::PingICMP is designed to mimic the ICMP ping functionality of Net::Ping, but because Win32::PingICMP uses ICMP.DLL instead of raw sockets, it will work without local Administrative privileges under Windows NT/2000/XP. In addition, it supports:
Win32::PingICMP
Net::Ping
ICMP.DLL
access to the ICMP_ECHO_REPLY data structure, making it possible to get more accurate timing values from pings
ICMP_ECHO_REPLY
setting the TTL, TOS, and IP Header Flags fields
operation in an asynchronous mode
This module requires Aldo Calpini's Win32::API, available from CPAN and via PPM, Win32::Event, included with the ActivePerl distribution, and Data::BitMask, available from CPAN.
Win32::API
Win32::Event
Data::BitMask
Toby Ovod-Everett, toby@ovod-everett.org
Some of the documentation is copied from that for Net::Ping 2.02. Since I was attempting to make this a replacement for that module, similarity in documentation struck me as a Good Thing(TM).
I would never have done this if I hadn't seen http://perlmonks.thepen.com/42739.html. I would never have attempted this if Win32::API didn't bring the Win32 API within the reach of mere mortals like me.
I would never have seen that if Christopher Elkin hadn't tried using Win32::ProcFarm on his web server to do monitoring via pings and asked me why things weren't working when the code ran without admin privs.
Win32::ProcFarm
Win32::PingICMP->new([$proto [, $def_timeout [, $bytes]]]);
Create a new ping object. All of the parameters are optional. $proto specifies the protocol to use when doing a ping. The only currently supported choice is 'icmp'.
$proto
icmp
If a default timeout ($def_timeout) in seconds is provided, it is used when a timeout is not given to the ping() method (below). It is recommended that the timeout be greater than 0 and the default, if not specified, is 5 seconds. Fractional values are permitted.
$def_timeout
ping()
0
5
If the number of data bytes ($bytes) is given, that many data bytes are included in the ping packet sent to the remote host. The default is 0 bytes. The maximum is 996.
$bytes
996
$p->ping($host [, $timeout [, %options]]);
Ping the remote host and wait for a response. $host can be either the hostname or the IP number of the remote host. The optional timeout should be greater than 0 seconds and defaults to whatever was specified when the ping object was created. Fractional values are permitted for the timeout. The %options hash accepts values for ttl, tos, and flags. If any of the values are specified, the other values default to 0, so you may want to specify them as well (especially ttl!). If none are specified, then they default to whatever the Windows defaults are (I don't have a packet sniffer or the expertise to determine them).
$host
%options
ttl
tos
flags
Hostname resolution is done via gethostbyname. If the hostname cannot be found or there is a problem with the IP number, undef is returned. Otherwise, 1 is returned if the host is reachable and 0 if it is not. For all practical purposes, undef and 0 and can be treated as the same case.
undef
1
$p->ping_async($host [, $timeout [, %options]]);
Initiates an asynchronous ping to a remote host. Only one asynchronous ping can be run at a time per Win32::PingICMP object, but you can have multiple Win32::PingICMP objects to enable parallel pinging. See ping for an overview of the parameters.
ping
$p->wait([$timeout]);
Used in conjunction with ping_async to wait for a response. Pass the timeout for which the Win32::PingICMP object should wait for the response during this call. Multiple calls to wait are permissible, as is a timeout value of 0. The call will return 0 if the ping is still outstanding and 1 is a response has been received or the ping timeout exceeded. Once a 1 has been returned from a call to wait, you can call details to get the response information. Use $p->details()->success() to get a value that mirrors the return value from ping.
ping_async
wait
details
$p->details()->success()
$p->close();
Close the network connection for this ping object. The network connection is also closed by "undef $p". The network connection is automatically closed if the ping object goes out of scope.
undef $p
$p->requestdata([$requestdata]);
Get and/or set the request data to be used in the packet.
$p->details();
Returns the gory details of the last ping attempted by the object. This is a reference to an anonymous hash and contains:
This is a reference to an anonymous array containing anonymous hash references with the gory details of the replies to the ping. In certain pathological cases, it might be possible for there to be multiple replies, which is why this is an array. This would be the case if the IcmpSendEcho call returned a value greater than 1, indicating that more than one packet was received in response. Of course, the first packet received should cause IcmpSendEcho to return, so I'm not quite sure how this would happen. The Microsoft documentation is incomplete on this point - they clearly state "Upon return, the buffer contains an array of ICMP_ECHO_REPLY structures followed by options and data." This would seem to indicate that multiple ICMP_ECHO_REPLY structures might reasonably be expected, as does the comment "The call returns when the time-out has expired or the reply buffer is filled." However, the functions appears to return as soon as there is one entry in the reply buffer, even when there is copious space left in the reply buffer and the time-out has yet to expire. My best guess is that there will never be more than one ICMP_ECHO_REPLY structure returned, but I have written the code to deal with the multiple structure case should it occur.
IcmpSendEcho
The anonymous hashes consist of the following elements:
Address from which the reply packet was sent.
Data present in the reply packet.
IP header flags from the reply packet.
Bytes from the options area following the IP header.
Round trip time. This appears to be inaccurate if there is no actual reply packet (as in the case of a 'IP_REQ_TIMED_OUT').
IP_REQ_TIMED_OUT
The per reply status returned by the IcmpSendEcho, returned as a text string constant.
The type-of-service for the reply packet.
The time-to-live for the reply packet.
The originally specified IP address or DNS name from the ping call.
The IP address used for the actual ping.
The roundtriptime value for the first reply.
roundtriptime
The status value for the first reply.
status
The same value returned by the ping call. This is absent if an IP address could not be determined for the host, 1 if there were one or more replies with a status value of 'IP_STATUS', and 0 if there were none.
IP_STATUS
The specified timeout value in milliseconds.
To install Win32::PingICMP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Win32::PingICMP
CPAN shell
perl -MCPAN -e shell install Win32::PingICMP
For more information on module installation, please visit the detailed CPAN module installation guide.