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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.