use warnings; use Getopt::Std; use Net::SNMP; use Data::Dumper; use Net::Ping; use Parallel::ForkManager qw( ); use Fcntl qw(:flock); use Socket qw( inet_aton ); #Declare the variables my $desc = '1.3.6.1.2.1.1.5.0'; my $host; my $pingable = "pingable.txt"; my $numOfProc = 25; my $manager = Parallel::ForkManager->new($numOfProc); #declare the desired host range my @sn_array = ("7.3.1."); my ($session, $error, %pdesc, $respnse); #delete the previously created file if (-e "pingable.txt"){ unlink "pingable.txt"; } #ping the host array and write to a file foreach $host (@sn_array){ for ($s=1; $s<255; $s++){ $manager->start and next; my $p = Net::Ping->new("icmp"); $host = $host . $s; print "pinging $host\n"; if ($p->ping($host)){ open PingFile, ">>", "$pingable" or die $!; flock(PingFile, LOCK_EX); print PingFile "$host\n"; flock(PingFile, LOCK_UN); close(PingFile); } $manager->finish; } } sleep(5); #count the new host ips in the file my $pingCount; open PINGFILE, "<", "$pingable"; $pingCount++ while(); close PINGFILE; # if there are new hosts in the file, scan them Cisco HostName OID and print it out if ($pingCount > 1) { open PINGFILE, "<", "$pingable"; while() { chomp; push @file, $_; } foreach my $host (@file) { &snmpwalk($host); } } #sub to get the Cisco host name OID and print it out sub snmpwalk{ ($session, $error) = Net::SNMP->session( -hostname => shift, -community => "public", -timeout => "5", -port => "161"); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $response = $session->get_request($desc); if (!defined $response) { printf "ERROR: Get request failed for host '%s': %s.\n", $session->hostname(), $session->error(); return; } my %pdesc = %{$response}; for my $key ( keys %pdesc ) { my $value = $pdesc{$key}; print "$value\n"; } my $err = $session->error; if ($err){ return 1; } }