[pgh-pm] perl net snmp

Malcolm Nooning m.nooning at comcast.net
Fri Jan 22 08:21:55 PST 2010


Obviously your script is letting you know when get_request returns an 
undefined value.  If get_request is supposed to give back that undefined 
value when there is nothing there (i.e. when106 is hit), well, then your 
script is correct.  If that is the case, then perhaps you may want to 
first decide on the full range of your possible Cisco switches to check 
for.  Say for example, 1 to 256, and traverse all numbers until all 255 
indexes have been checked.  Along the way you could printf or otherwise 
collect only the ones whose get_request returned a defined value.

I hope I understood your question correctly.

G.Pitman wrote:
> Hi,
> I used to use perl a bit but I am very rusty. I am trying to walk the 
> ifIndex portion of a cisco switch so that I can either get the last 
> index number or build an array of index numbers to use for queries.
>
> I have tried using a get_request in a loop until I get an error but 
> there is a break in list. When I do an snmpwalk I see index numbers 
> from 1-105 and 154-224 with the break in the middle so my script stops 
> at 105 indicating that it is the last. If I use get_next_request it 
> does not error out. Can anyone guide my failing logic?
>
> ---------
>
>
>
> #!/usr/bin/perl -w
>
> use Net::SNMP;
> my $OID_vLanID = '1.3.6.1.2.1.2.2.1.1.';
>
> my ($session, $error) = Net::SNMP->session(
>       -hostname  => 'SWITCH_IP',
>       -community => 'public'
>    );
>
> if (!defined $session) {
>       printf "ERROR: %s.\n", $error;
>       exit 1;
>    }
> $foundLast = 0;
> $item = 1;
> while ($foundLast != 1)
> {
>         $getValue = "$OID_vLanID" . "$item";
>         $result = $session->get_request(-varbindlist => [ $getValue ],);
>         if (!defined $result)
>         {
>                 $item--;
>                 printf "ITEM is $item-- -- ERROR: %s.\n", 
> $session->error();
>                 $foundLast = 1;
>                 $session->close();
>         }
>         else
>         {
>         $item++;
>         }
> }
>
> print "Total: $item\n";
>    $session->close();
>
> exit 0;
> ------------------------------------------------------------------------
>
> _______________________________________________
> pgh-pm mailing list
> pgh-pm at pm.org
> http://mail.pm.org/mailman/listinfo/pgh-pm


More information about the pgh-pm mailing list