CongoGrey has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am still pretty new to the Perl world and I can not seem to figure out what I am doing wrong in my script. I am trying to do a SNMP get next request and store the returned values in either an array or hash so i can look at the values. The problem is when I run my script I get the following output:
Net::SNMP=HASH(0x246bdc)
I do a SNMP walk on my router and I get the following:
ip.ipAddrTable.ipAddrEntry.ipAdEntIfIndex.192.1.1.1 : INTEGER: 1 ip.ipAddrTable.ipAddrEntry.ipAdEntIfIndex.192.1.1.2 : INTEGER: 42 ip.ipAddrTable.ipAddrEntry.ipAdEntIfIndex.192.1.1.3 : INTEGER: 0 ip.ipAddrTable.ipAddrEntry.ipAdEntIfIndex.192.1.1.4 : INTEGER: 47
Here is the script so far:
#!/usr/local/bin/perl -w use strict; use Net::SNMP; my @vars; my $snmp; $snmp = Net::SNMP->session( -hostname => '192.3.3.1' +, -community => 'public', -port => '16 +2', ); @vars = $snmp->get_next_request( -varbindlist => '.1.3.6.1.2.1.4.20.1.2 +', ); $snmp->close; print @vars;
Any help would be great!!

Replies are listed 'Best First'.
Re: Storing SNMP Next Request into a Hash or Array
by Mr. Muskrat (Canon) on May 13, 2003 at 13:46 UTC
    get_next_request() returns a hash reference. Try something like the following untested code:
    my @oids = ( '.1.3.6.1.2.1.4.20.1.2' ); my $req = $snmp->get_next_request( -varbindlist => \@oids, ); $snmp->close; while (my ($key,$value) = each %{$req}) { print "$key = $value\n"; }

    Thanks to zby for pointing out the array reference.

      Thanks Muskrat and zby you helped steer me into the light. I have one more question and I think that this will make my script very large. Below is the script so far and it looks but it will not 'walk' the MIB table it only gets the first responce and then stops. I would have thought that the get_next_request would have built into it a way to keep going until the last entry of the MIB table.
      What i am basically looking for is what is the best looping mechanism for this purpose?
      #!/usr/local/bin/perl -w use strict; use Net::SNMP; use Data::Dumper; my $snmp = Net::SNMP->session( -hostname => '192.3.3.1' +, -community => 'public', -port => '161', ); my @oid; $oid[0]='.1.3.6.1.2.1.4.20.1.2'; my @vars = $snmp->get_next_request( -varbindlist => \@oid, ); my $error = $snmp->error; $snmp->close; print Dumper(\@vars); print Dumper(\@oid); print "the OID array is\n"; print "@oid\n"; print "The Error??\n"; print "$error\n";
        Okay i am very close now I can now walk the complete MIB but, my Perl script does not seem to let me store the information into a hash:
        my %var = $snmp->get_table( -baseoid => $oid);
        Gives me an error:
        Reference found where even-sized list expected
        But, if I make the 'var' variable a '$' and use Dumper the information is there:
        $VAR1 = [ { '.1.3.6.1.2.1.4.20.1.2.10.61.17.1' => '0', '.1.3.6.1.2.1.4.20.1.2.10.61.10.1' => '42', '.1.3.6.1.2.1.4.20.1.2.10.76.7.1' => '47', '.1.3.6.1.2.1.4.20.1.2.10.61.0.126' => '1' } ];
Re: Storing SNMP Next Request into a Hash or Array
by zby (Vicar) on May 13, 2003 at 13:41 UTC
    Try: use Data::Dumper at the beginning of your code and then print Dumper(\@vars) at the end. This should give you a clue on what is in the @vars array. By the way the doc for Net::SNMP says the -varbindlist parameter should be a reference to an array: -varbindlist      => \@oids not a string, I have never used this module so I can't tell, but it seems suspicious.