use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case); #bundling
use Pod::Usage;
use threads;
use Net::Ping;
########
my %opts;
my ($opt_help, $opt_man);
GetOptions(
'start=i' => \$opts{'start'},
'Stop=i' => \$opts{'stop'},
'Threads=i' => \$opts{'threads'},
'help!' => \$opt_help,
'man!' => \$opt_man,
) or pod2usage(-verbose => 0);
if (!@ARGV) {
pod2usage(-verbose => 0, -message => "$0: network required\n")
}
########
if (!defined($opts{'start'})) { $opts{'start'} = 1 }
if (!defined($opts{'stop'})) { $opts{'stop'} = 254 }
if (!defined($opts{'threads'})) { $opts{'threads'} = 5 }
if ($opts{'start'} > $opts{'stop'}) {
print "$0: start is greater than stop\n";
exit 1
}
my @th;
my $count = 0;
for (@ARGV) {
if ($_ !~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.\d{1,3}$/) {
print "$0: $_ is not valid network, skipping ...\n";
next
}
$opts{'net'} = $1 . "." . $2 . "." . $3 . ".";
print "Pinging network: $opts{'net'}0\n";
$th[$count++] = threads->create(\&THPING, \%opts)
}
for (@th) {
$_->join
}
sub THPING {
my ($args) = @_;
my @th;
my $count = 0;
for ($args->{'start'}..$args->{'stop'}) {
print " Pinging host: $args->{'net'}$_\n";
$th[$count++] = threads->create(\&PINGIT, $args->{'net'} . $_)
}
for (@th) {
$_->join
}
return 0
}
sub PINGIT {
my ($addr) = @_;
my $p = Net::Ping->new();
if ($p->ping($addr)) {
print "$addr is alive.\n"
} else {
print "$addr is DEAD.\n"
}
$p->close();
return 0
}
####
{C} > test 172.18.7.0 172.18.6.0 -s 31 -S 35
Pinging network: 172.18.7.0
Pinging host: 172.18.7.31
Pinging host: 172.18.7.32
Pinging network: 172.18.6.0
Pinging host: 172.18.7.33
Pinging host: 172.18.7.34
Pinging host: 172.18.7.35
Pinging host: 172.18.6.31
Pinging host: 172.18.6.32
Pinging host: 172.18.6.33
Pinging host: 172.18.6.34
Pinging host: 172.18.6.35
172.18.7.31 is alive.
172.18.7.33 is alive.
172.18.7.34 is alive.
172.18.7.32 is alive.
172.18.7.35 is alive.
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
172.18.6.33 is DEAD.
172.18.6.31 is DEAD.
172.18.6.34 is DEAD.
172.18.6.35 is DEAD.
172.18.6.32 is DEAD.
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
Scalars leaked: 1
####
{C} > ver
Microsoft Windows XP [Version 5.1.2600]
{C} > perl -v | head -8
This is perl, v5.10.1 built for MSWin32-x86-multi-thread
(with 2 registered patches, see perl -V for more detail)
Copyright 1987-2009, Larry Wall
Binary build 1006 [291086] provided by ActiveState http://www.ActiveState.com
Built Aug 24 2009 13:48:26
{C} > perl -V | grep threads
useithreads=define, usemultiplicity=define