This script will run snmp queries on multiple routers at a time, its working fine, but memory has me worried. just for testing i scanned a non existan routers so they timed out, if it times out then the script will do nothing but keep on scanning for a live router. so i scanned from 10.0.0.0 to 10.0.255.255 and ram was increasing from 10mb to 60mb when scanning was at 10.0.100.0. that cant be right, can it? i mean it has actually done nothing but to scan and return "time out" for all these ips. so what is using so much ram?? it might be the sessions hash, but ive tried closing them but they still seem to be open. can i do something to avoid such amounts of ram being used??
#!/usr/bin/perl -w use warnings; use strict; use Net::SNMP qw(snmp_dispatcher oid_lex_sort); #use Smart::Comments '###'; my $startip = $ARGV[0] || die "Missing Starting IP"; my $endip = $ARGV[1] || die "Missing Ending IP"; my $community = $ARGV[2] || die "Missing community string"; my @ip_start = split(/\./,$startip); my @ip_end = split(/\./,$endip); my $contadr = 0; my($i,$j,$k,$l); { for ($l="$ip_start[0]";$l<="$ip_end[0]";$l++){ for ($i="$ip_start[1]";$i<="$ip_end[1]";$i++){ for ($j="$ip_start[2]";$j<="$ip_end[2]";$j++){ for ($k="$ip_start[3]";$k<="$ip_end[3]";$k++){ my ($session,$error)=Net::SNMP->session(-hostname=>"$l.$i.$j.$k", -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,"$l.$i.$j.$k"] ); $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; { sub getms { my $obj = shift; my $session = shift; my $hfcip = shift; if (!defined($obj->var_bind_list)) { warn "$hfcip SNMP Error.",$obj->error(),"\n"; return; }; ## print values for the oids }; };

In reply to 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.