Instead of using four nested for loops you could do it like this:

#!/usr/bin/perl use warnings; use strict; use Net::SNMP qw(snmp_dispatcher oid_lex_sort); #use Smart::Comments '###'; use Socket; my $startip = $ARGV[ 0 ] or die 'Missing Starting IP'; my $endip = $ARGV[ 1 ] or die 'Missing Ending IP'; my $community = $ARGV[ 2 ] or die 'Missing community string'; my $ip_start = unpack 'N', inet_aton $startip; my $ip_end = unpack 'N', inet_aton $endip; my $contadr = 0; for my $i ( $ip_start .. $ip_end ) { my $ip = inet_ntoa pack 'N', $i my ( $session, $error ) = Net::SNMP->session( -hostname => $ip, -version => 'snmpv2c', -nonblocking => 1, -community => $community, -timeout => 3, -retries => 1, ); if ( defined $session ) { my $serialno = '.1.3.6.1.3.83.1.1.4.0'; my $mac = '.1.3.6.1.2.1.2.2.1.6.2'; my @msoids = ( $hfcmac, $serialno ); my $result = $session->get_request( -varbindlist => \@msoids, callback => [ \&getms, $session, $ip ] ); $session->close; ## reason for all this close, is that they dont seem to get #closed no + matter where i put them } else { print "Session not defined! $error\n"; } $session->close; snmp_dispatcher(); } exit;

In reply to Re: How to improve memory usage in this script?? by jwkrahn
in thread How to improve memory usage in this script?? by Anonymous Monk

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.