Category: | Networking |
Author/Contact Info | Donald Reckinger modified ping script by Randal Schwartz |
Description: | Pings all hosts in C Class subnets array and resolves hostnames in DNS. |
#!/usr/bin/perl
use Net::Ping;
use Socket;
#*********************************************************************
# pinger : Ping C-Class Ip's in the following subnets (@subnets)
#
# Description: ping hosts in the listed subnets and report to STDOUT
#
# Modified an existing pid fork ping script written by Randal Scwartz
# Octet range listed is set for my building removed
# broadcast octets as well as unused hosts in low range.
#
# Donald J. Reckinger Mon Aug 5 15:57:04 EDT 2002
#*********************************************************************
@subnets = (1,2,3,4,5,6,7);
foreach $subnet (@subnets) {
for ($octet=20; $octet <= 254; $octet++) {
push @hosts, "192.168.$subnet.$octet";
}
}
sub ping_a_host {
$p = Net::Ping->new("icmp");
($p->ping($host,2)) =~ /1/ ? 1 : 0;
$p->close();
}
my %pid_to_host;
my %host_result;
sub wait_for_a_kid {
$p = Net::Ping->new("icmp");
my $pid = wait;
return 0 if $pid < 0;
my $host = delete $pid_to_host{$pid}
or warn("Why did I see $pid ($?)\n"), next;
warn "stopping $pid for $host\n";
$host_result{$host} = ($p->ping($host,2)) =~ /1/ ? 1 : 0;
1;
}
for (@hosts) {
wait_for_a_kid() if keys %pid_to_host > 10;
if (my $pid = fork) {
## parent does...
$pid_to_host{$pid} = $_;
warn "$pid is processing $_\n";
} else { # child does
## child does...
exit !ping_a_host($_);
}
}
## final reap:
1 while wait_for_a_kid();
for (sort keys %host_result) {
chomp(my $claimed_hostname = gethostbyaddr(inet_aton($_ ),AF_INET));
print "$_ aka $claimed_hostname is ", ($host_result{$_} ? "good" : "ba
+d"), "\n";
}
|
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scan C Subnets for /up/down hosts and resolve names in DNS
by rob_au (Abbot) on Sep 04, 2002 at 22:07 UTC | |
by /dev/null (Chaplain) on Sep 04, 2002 at 23:38 UTC |