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 }