dev/perl/ping.pl

42 lines
626 B
Perl

#!/usr/bin/perl
use threads;
use Net::Ping;
$subnet='192.168.2.';
@hosts=(1..254);
sub subnetting {
my $ip00 = shift @_;
my @ip01 = @_;
my @ipaddr;
for (my $k=0; $k<=$#ip01; $k++) {
$ipaddr[$k]=$ip00.$ip01[$k];
}
return @ipaddr;
}
sub startping {
my $host = $_;
my $p = Net::Ping->new("tcp",1,1);
if ($p->ping($host)) {
print "$host\n";
}
$p->close();
}
sub startthreads {
foreach (@ipaddr) {
sleep(0);
push(@pinged_addr, threads->create("startping", $_));
}
foreach (@pinged_addr) {
$_->join();
}
}
@ipaddr = subnetting($subnet, @hosts);
startthreads(@ipaddr);