Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created July 31, 2015 22:10
Show Gist options
  • Save p120ph37/3789ba7d8d7431ec88f1 to your computer and use it in GitHub Desktop.
Save p120ph37/3789ba7d8d7431ec88f1 to your computer and use it in GitHub Desktop.
Example using Win32::PingICMP
use warnings;
use strict;
use Win32::PingICMP;
use Socket qw/inet_ntoa/;
# The exact error strings as used by ping.exe
my %errors = (
'IP_BUF_TOO_SMALL' => 'General failure.',
'IP_DEST_NET_UNREACHABLE' => 'Destination net unreachable.',
'IP_DEST_HOST_UNREACHABLE' => 'Destination host unreachable.',
'IP_DEST_PROT_UNREACHABLE' => 'Destination protocol unreachable.',
'IP_DEST_PORT_UNREACHABLE' => 'Destination port unreachable.',
'IP_NO_RESOURCES' => 'No resources.',
'IP_BAD_OPTION' => 'Bad option specified.',
'IP_HW_ERROR' => 'Hardware error.',
'IP_PACKET_TOO_BIG' => 'Packet needs to be fragmented but DF set.',
'IP_REQ_TIMED_OUT' => 'Request timed out.',
'IP_BAD_REQ' => 'General failure.',
'IP_BAD_ROUTE' => 'Invalid source route specified.',
'IP_TTL_EXPIRED_TRANSIT' => 'TTL expired in transit.',
'IP_TTL_EXPIRED_REASSEM' => 'TTL expired during reassembly.',
'IP_PARAM_PROBLEM' => 'IP parameter problem.',
'IP_SOURCE_QUENCH' => 'Source quench received.',
'IP_OPTION_TOO_BIG' => 'Specified option is too large.',
'IP_BAD_DESTINATION' => 'Destination specified is invalid.',
);
my $host = $ARGV[0] or die "Usage: $0 HOST\n";
my $data = 'abcdefghijklmnopqrstuvwabcdefghi'; # that's actually what Windows uses as a default payload.
my $timeout = 5; # timeout value is in seconds
my $flags = 0; # 2="don't fragment"
my $tos = 0;
my $ttl = 128;
my $ip = inet_ntoa((gethostbyname($host))[4]);
printf(
"\nPinging %s with %s bytes of data:\n\n",
($host eq $ip ? $host : "$host [$ip]"),
length($data)
);
my $p = Win32::PingICMP->new();
$p->{'RequestData'} = $data;
my $success = $p->ping($ip, $timeout, flags=>$flags, tos=>$tos, ttl=>$ttl);
if($success) {
my $reply = $p->details->{'replies'}->[0];
printf(
"Reply from %s: bytes=%d time=%sms TTL=%s\n",
$reply->{'address'},
length($reply->{'data'}),
$reply->{'roundtriptime'} || '<1',
$reply->{'ttl'}
);
} else {
my $reply = $p->details->{'replies'}->[0];
printf(
"Reply from %s: %s\n",
$reply->{'address'},
$errors{$reply->{'status'}} || 'General failure.'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment