| Net-Ping-External documentation | Contained in the Net-Ping-External distribution. |
Net::Ping::External - Cross-platform interface to ICMP "ping" utilities
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);
Net::Ping::External is a module which interfaces with the "ping" command
on many systems. It presently provides a single function, 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?
This module is still "alpha"; it is expected that more options to the ping()
function will be added soon.
host, hostnameThe 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.
ipA packed bit-string representing the 4-byte packed IP address (as
returned by Socket.pm's inet_aton() function) of the host that you
would like to ping.
timeoutThe maximum amount of time, in seconds, that ping() will wait for a response.
If the remote system does not respond before the timeout has elapsed, ping()
will return false.
Default value: 5.
countThe 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, ping() still returns just true or false.
Default value: 1.
sizeSpecifies 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.
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.
Tested OK on Win98, Win XP. It should work on other Windows systems as well.
Tested OK on Cygwin 1.5.21. Problem is that we may be running windows ping. They have different options.
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
count are disabled.
Tested OK on OpenBSD 2.7 and 3.0, Netbsd 1.5.3, Freebsd 4.6.2, 5.4. Needs testing for BSDi.
Tested OK on Solaris 2.6 and 2.7.
Tested OK on IRIX 6.5.
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.
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.
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.
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.
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.
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).
Net::Ping
| Net-Ping-External documentation | Contained in the Net-Ping-External distribution. |
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__