Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Scan C Subnets for /up/down hosts and resolve names in DNS

by /dev/null (Chaplain)
on Sep 04, 2002 at 14:51 UTC ( [id://195099]=sourcecode: print w/replies, xml ) Need Help??
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
    It may be worth having a look at my rant on the iteration through network hosts by making use of the Net::Netmask module here - Never again should your network administration scripts be non-portable! :-)

     

      Thanks rob_au! I wasn't aware of this module. I probably won't change anything with a small script like this. What is easier? I could change a few numbers or pack the script with the Net::Netmask module so the code is completely portable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://195099]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found