/usr/share/perl5/Net/Ping/External.pm is in libnet-ping-external-perl 0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | package Net::Ping::External;
# Author: Colin McMillen (colinm AT cpan.org)
# See also the CREDITS section in the POD below.
#
# Copyright (c) 2001-2003 Colin McMillen. All rights reserved. This
# program is free software; you may redistribute it and/or modify it
# under the same terms as Perl itself.
# Copyright (c) 2006-2008 Alexandr Ciornii
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
use Carp;
use Socket qw(inet_ntoa);
require Exporter;
$VERSION = "0.13";
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(ping);
sub ping {
# Set up defaults & override defaults with parameters sent.
my %args = (count => 1, size => 56, @_);
# "host" and "hostname" are synonyms.
$args{host} = $args{hostname} if defined $args{hostname};
# If we have an "ip" argument, convert it to a hostname and use that.
$args{host} = inet_ntoa($args{ip}) if defined $args{ip};
# croak() if no hostname was provided.
croak("You must provide a hostname") unless defined $args{host};
$args{timeout} = 5 unless defined $args{timeout} && $args{timeout} > 0;
my %dispatch =
(linux => \&_ping_linux,
mswin32 => \&_ping_win32,
cygwin => \&_ping_cygwin,
solaris => \&_ping_solaris,
bsdos => \&_ping_bsdos,
beos => \&_ping_beos,
hpux => \&_ping_hpux,
dec_osf => \&_ping_dec_osf,
bsd => \&_ping_bsd,
darwin => \&_ping_darwin,
openbsd => \&_ping_unix,
freebsd => \&_ping_freebsd,
next => \&_ping_next,
unicosmk => \&_ping_unicosmk,
netbsd => \&_ping_netbsd,
irix => \&_ping_unix,
aix => \&_ping_aix,
);
my $subref = $dispatch{lc $^O};
croak("External ping not supported on your system") unless $subref;
return $subref->(%args);
}
# Win32 is the only system so far for which we actually need to parse the
# results of the system ping command.
sub _ping_win32 {
my %args = @_;
$args{timeout} *= 1000; # Win32 ping timeout is specified in milliseconds
#for each ping
my $command = "ping -l $args{size} -n $args{count} -w $args{timeout} $args{host}";
print "$command\n" if $DEBUG;
my $result = `$command`;
return 1 if $result =~ /time.*ms/;
return 1 if $result =~ /TTL/;
return 1 if $result =~ /is alive/; # ppt (from CPAN) ping
# return 1 if $result !~ /\(100%/; # 100% packages lost
return 0;
}
# Mac OS X 10.2 ping does not handle -w timeout now does it return a
# status code if it fails to ping (unless it cannot resolve the domain
# name)
# Thanks to Peter N. Lewis for this one.
sub _ping_darwin {
my %args = @_;
my $command = "ping -s $args{size} -c $args{count} $args{host}";
my $devnull = "/dev/null";
$command .= " 2>$devnull";
print "$command\n" if $DEBUG;
my $result = `$command`;
return 1 if $result =~ /(\d+) packets received/ && $1 > 0;
return 0;
}
# Generic subroutine to handle pinging using the system() function. Generally,
# UNIX-like systems return 0 on a successful ping and something else on
# failure. If the return value of running $command is equal to the value
# specified as $success, the ping succeeds. Otherwise, it fails.
sub _ping_system {
my ($command, # The ping command to run
$success, # What value the system ping command returns on success
) = @_;
my $devnull = "/dev/null";
$command .= " 1>$devnull 2>$devnull";
print "#$command\n" if $DEBUG;
my $exit_status = system($command) >> 8;
return 1 if $exit_status == $success;
return 0;
}
# Below are all the systems on which _ping_system() has been tested
# and found OK.
# Assumed OK for DEC OSF
sub _ping_dec_osf {
my %args = @_;
my $command = "ping -c $args{count} -s $args{size} -q -u $args{host}";
return _ping_system($command, 0);
}
# Assumed OK for unicosmk
sub _ping_unicosmk {
my %args = @_;
my $command = "ping -s $args{size} -c $args{count} $args{host}";
return _ping_system($command, 0);
}
# NeXTStep 3.3/sparc
sub _ping_next {
my %args = @_;
my $command = "ping $args{host} $args{size} $args{count}";
return _ping_system($command, 0);
}
# Assumed OK for HP-UX.
sub _ping_hpux {
my %args = @_;
my $command = "ping $args{host} $args{size} $args{count}";
return _ping_system($command, 0);
}
# Assumed OK for BSD/OS 4.
sub _ping_bsdos {
my %args = @_;
my $command = "ping -c $args{count} -s $args{size} $args{host}";
return _ping_system($command, 0);
}
# Assumed OK for BeOS.
sub _ping_beos {
my %args = @_;
my $command = "ping -c $args{count} -s $args{size} $args{host}";
return _ping_system($command, 0);
}
# Assumed OK for AIX
sub _ping_aix {
my %args = @_;
my $command = "ping -c $args{count} -s $args{size} -q $args{host}";
return _ping_system($command, 0);
}
# OpenBSD 2.7 OK, IRIX 6.5 OK
# Assumed OK for NetBSD & FreeBSD, but needs testing
sub _ping_unix {
my %args = @_;
my $command = "ping -s $args{size} -c $args{count} -w $args{timeout} $args{host}";
return _ping_system($command, 0);
}
sub _locate_ping_netbsd {
return '/usr/sbin/ping' if (-x '/usr/sbin/ping');
return 'ping';
}
sub _ping_netbsd {
my %args = @_;
my $command = _locate_ping_netbsd()." -s $args{size} -c $args{count} -w $args{timeout} $args{host}";
return _ping_system($command, 0);
}
#-s size -c count -w timeout
#http://netbsd.gw.com/cgi-bin/man-cgi?ping++NetBSD-current
# Assumed OK for FreeBSD 3.4
# -s size option supported -- superuser only... fixme
sub _ping_bsd {
my %args = @_;
my $command = "ping -c $args{count} -q $args{hostname}";
return _ping_system($command, 0);
}
# Debian 2.2 OK, RedHat 6.2 OK
# -s size option available to superuser... FIXME?
sub _ping_linux {
my %args = @_;
my $command;
#for next version
if (-e '/etc/redhat-release' || -e '/etc/SuSE-release') {
$command = "ping -c $args{count} -s $args{size} $args{host}";
} else {
$command = "ping -c $args{count} $args{host}";
}
return _ping_system($command, 0);
}
# Solaris 2.6, 2.7 OK
sub _ping_solaris {
my %args = @_;
my $command = "ping -s $args{host} $args{size} $args{timeout}";
return _ping_system($command, 0);
}
# FreeBSD. Tested OK for Freebsd 4.3
# -s size option supported -- superuser only... FIXME?
# -w timeout option for BSD replaced by -t
sub _ping_freebsd {
my %args = @_;
my $command = "ping -c $args{count} -t $args{timeout} $args{host}";
return _ping_system($command, 0);
}
#No timeout
#Usage: ping [-dfqrv] host [packetsize [count [preload]]]
sub _ping_cygwin {
my %args = @_;
my $command = "ping $args{host} $args{size} $args{count}";
return _ping_system($command, 0);
}
#Problem is that we may be running windows ping
1;
__END__
=head1 NAME
Net::Ping::External - Cross-platform interface to ICMP "ping" utilities
=head1 SYNOPSIS
In general:
use Net::Ping::External qw(ping);
ping(%options);
Some examples:
use Net::Ping::External qw(ping);
# Ping a single host
my $alive = ping(host => "127.0.0.1");
print "127.0.0.1 is online" if $alive;
# Or a list of hosts
my @hosts = qw(127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4);
my $num_alive = 0;
foreach (@hosts) {
$alive = ping(hostname => $_, timeout => 5);
print "$_ is alive!\n" if $alive;
$num_alive++;
}
print "$num_alive hosts are alive.\n";
# Using all the fancy options:
ping(hostname => "127.0.0.1", count => 5, size => 1024, timeout => 3);
=head1 DESCRIPTION
Net::Ping::External is a module which interfaces with the "ping" command
on many systems. It presently provides a single function, C<ping()>, that
takes in a hostname and (optionally) a timeout and returns true if the
host is alive, and false otherwise. Unless you have the ability (and
willingness) to run your scripts as the superuser on your system, this
module will probably provide more accurate results than Net::Ping will.
Why?
=over 4
=item *
ICMP ping is the most reliable way to tell whether a remote host is alive.
=item *
However, Net::Ping cannot use an ICMP ping unless you are running your
script with privileged (AKA "root") access.
=item *
The system's "ping" command uses ICMP and does not usually require
privileged access.
=item *
While it is relatively trivial to write a Perl script that parses the
output of the "ping" command on a given system, the aim of this module
is to encapsulate this functionality and provide a single interface for
it that works on many systems.
=back
=head2 ping() OPTIONS
This module is still "alpha"; it is expected that more options to the C<ping()>
function will be added soon.
=over 4
=item * C<host, hostname>
The hostname (or dotted-quad IP address) of the remote host you are trying
to ping. You must specify either the "hostname" option or the "ip" option.
"host" and "hostname" are synonymous.
=item * C<ip>
A packed bit-string representing the 4-byte packed IP address (as
returned by C<Socket.pm>'s C<inet_aton()> function) of the host that you
would like to ping.
=item * C<timeout>
The maximum amount of time, in seconds, that C<ping()> will wait for a response.
If the remote system does not respond before the timeout has elapsed, C<ping()>
will return false.
Default value: 5.
=item * C<count>
The number of ICMP ping packets to send to the remote host. Eventually,
Net::Ping::External will return the number of packets that were acknowledged
by the remote host; for now, however, C<ping()> still returns just true or false.
Default value: 1.
=item * C<size>
Specifies the number of data bytes to be sent. The default is
56, which translates into 64 ICMP data bytes when combined with
the 8 bytes of ICMP header data.
Default value: 56.
=back
=head2 SUPPORTED PLATFORMS
Support currently exists for interfacing with the standard ping
utilities on the following systems. Please note that the path to the `ping'
should be somewhere in your PATH environment variable (or your system's
closest equivalent thereof.) Otherwise, Net::Ping::External will be unable
to locate your system's `ping' command.
=over 4
=item * Win32
Tested OK on Win98, Win XP. It should work on other Windows systems as well.
=item * Cygwin
Tested OK on Cygwin 1.5.21. Problem is that we may be running windows ping.
They have different options.
=item * Linux
Tested OK on Debian 2.2 and Redhat 6.2. It appears that different versions
of Linux use different versions of ping, which support different options.
Not sure how I'm going to resolve this yet; for now, all the options but
C<count> are disabled.
=item * BSD
Tested OK on OpenBSD 2.7 and 3.0, Netbsd 1.5.3, Freebsd 4.6.2, 5.4. Needs testing for BSDi.
=item * Solaris
Tested OK on Solaris 2.6 and 2.7.
=item * IRIX
Tested OK on IRIX 6.5.
=item * AIX, DEC OSF, UNICOSMK, NeXTStep, HP-UX, BSD/OS (BSDi), BeOS
Support for these systems is integrated into this module but none have been
tested yet. If you have successful or unsuccessful test results for any of
these systems, please send them to me. On some of these systems, some of the
arguments may not be supported. If you'd like to see better support on your
system, please e-mail me.
=back
More systems will be added as soon as any users request them. If your
system is not currently supported, e-mail me; adding support to your
system is probably trivial.
=head1 BUGS
This module should be considered beta. Bugs may exist. Although no
specific bugs are known at this time, the module could use testing
on a greater variety of systems.
See the warning below.
=head1 WARNING
This module calls whatever "ping" program it first finds in your PATH
environment variable. If your PATH contains a trojan "ping" program,
this module will call that program. This involves a small amount of
risk, but no more than simply typing "ping" at a system prompt.
Beware Greeks bearing gifts.
=head1 AUTHOR
Alexandr Ciornii (alexchorny AT gmail.com), Colin McMillen (colinm AT cpan.org)
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=head1 CREDITS
Dan Moore contributed command-line options and code for NeXT, BeOS,
HP-UX, and BSD/OS.
Jarkko Hietaniemi contributed a huge list of command-line options and results
for the `ping' command on 9 different systems.
Randy Moore contributed several patches for Win32 support.
Marc-Andre Dumas contributed a patch for FreeBSD support.
Jonathan Stowe fixed a bug in 0.09 that prevented the module from
running on some systems.
Numerous people sent in a patch to fix a bug in 0.10 that broke ping on Windows systems.
Peter N. Lewis contributed a patch that works correctly on Mac OS X
10.2 (and hopefully other versions as well).
=head1 SEE ALSO
Net::Ping
=cut
|