#!/usr/bin/perl
use strict;
use warnings;
use Net::Ping;
my $down;
my $host = $ARGV[0];
my $host2 = $ARGV[1];
my $rc = ( PING($host) == 0 ) ? "$host is up" :
( PING($host2) == 0 ) ? "$host2 is up" :
"Neither $host or $host2 are reachable";
print "$rc\n";
sub PING
{
my $host = shift;
my $p = Net::Ping->new("icmp");
return ( $p->ping($host) ) ? 0 : 1;
}
####
From perldoc Net::Ping
If the "icmp" protocol is specified, the ping() method sends
an icmp echo message to the remote host, which is what the
UNIX ping program does. If the echoed message is received
from the remote host and the echoed information is correct,
the remote host is considered reachable. Specifying the
"icmp" protocol requires that the program be run as root or
that the program be setuid to root.
##
##
#!/usr/bin/perl
use strict;
use IO::Socket::INET;
use Getopt::Long;
my @hosts = "";
my $timeout = "2";
#my $port = "";
my @ports = "";
my $opts = GetOptions(
"host=s" => \@hosts,
"port=s" => \@ports,
"timeout=s" => \$timeout
);
@hosts = split /,/, join( ',', @hosts );
die "usage: sp -h [ -h ] -p [ -p ]\n" unless $hosts[1];
shift @ports;
for my $host (@hosts) {
next if $host eq '';
for my $port (@ports)
{
my $status = ( connection( $host, $port ) == 0 ) ? "Up" : "Down";
my $line = sprintf(" %-20s %-5s %-1s", $host, $port, $status);
print $line . "\n";
}
}
##############
sub connection
##############
{
my $host = shift;
my $tcp_port = shift;
return ( IO::Socket::INET->new(
Timeout => $timeout,
PeerAddr => $host,
PeerPort => $tcp_port,
Proto => 'tcp' ) ) ? 0 : 1;
}
perl sp.pl -h google.com -h yahoo.com -p 80
google.com 80 Up
yahoo.com 80 Up