I am trying to create a script that scans my network with ICMP ping and then, if the ping is successful, scans it for Cisco OID hostname. The idea is that when i am done scanning all IP subnets I only going to end up with Cisco devices, which then will be ported over to other network management solutions. The script is doing pretty good job, however, as it is written now, it is limited to 255 (\24) usable host IPs. I have to add a new subnet to the array each time to expand my scan. Is there any way it can be optimized to expand on \24 subnet range to \22 or \16? Thanks!
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(<PINGFILE>); 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(<PINGFILE>) { 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; } }
In reply to Scan multiple IP subnets by vlad3848
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |