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

My Environment:
I downloaded and installed net-snmp5.4.2.1-1.win32.exe from sourceforge (needed a binary for windows that would work on Perl v5.8.9). I followed the README instruction to install the Perl modules:
ppm remove NETSNMP
cd (install folder)\Perl
ppm install NetSNMP.ppd
Test the install:
net-snmp-perl-test.pl
Result:
Testing translateObj
********************
Test passed. Result: .1.3.6.1.2.1.1.1
I test that my perl module is installed correctly:
perl -MSNMP -e 1

Issue:
Right now I am just writing some basic code to get the number of interfaces, and then print that number. I am getting nothing returned when I call $session->get. Here is my code. I am passing in the ip and the community string.

use SNMP; $ENV{'MIBS'} = 'ALL'; &SNMP::initMib(); $session = new SNMP::Session(DestHost => $ARGV[0], Community =>$A +RGV[1]); $numInts = $session->get('1.3.6.1.2.1.2.1.0'); print "Number of Interfaces: $numInts\n";
When I run my code, I get:
Number of Interfaces:

I have tried a bunch of other things, but can't get it to return any values. If I just go to my commandline and type:
snmpget -v1 -c <community string> <ip> 1.3.6.1.2.1.2.1.0
I get results back as expected.....

Replies are listed 'Best First'.
Re: SNMP Perl Module methods not returning any values...
by Khen1950fx (Canon) on Sep 02, 2010 at 01:39 UTC
    To debug your script, you can start by adding verbose and debugging:
    #!/usr/bin/perl use strict; use warnings; use Devel::SimpleTrace; use SNMP; $SNMP::verbose = 1; $SNMP::debugging = 1; $ENV{'MIBS'} = 'ALL'; &SNMP::initMib(); my $session = new SNMP::Session(DestHost => $ARGV[0], Community =>$ARG +V[1]); my $numInts = $session->get('1.3.6.1.2.1.2.1.0'); print "Number of Interfaces: $numInts\n";
    That'll tell you why you're not getting back any values.

    Here's a different script with errors:

    #!/usr/bin/perl use strict; use warnings; use SNMP; my $hostname = 'localhost'; my $port = 161, my $community = 'public'; $SNMP::verbose = 1; $SNMP::debugging = 1; $SNMP::dump_packet = 1; my $session = new SNMP::Session( 'DestHost' => $hostname, 'Community' => $community, 'RemotePort' => $port, 'UseSprintValue' => 1 ); die "session creation error: Can't fire-up!\n" unless (defined $session); my $vars = new SNMP::VarList(['ipNetToMediaNetAddress'], ['ipNetToMediaPhysAddress']); my ($ip,$mac) = $session->getnext($vars); die $session->{ErrorStr} if ($session->{ErrorStr}); while (!$session->{ErrorStr} and $$vars[0]->tag eq "ipNetToMediaNetAddress") { print "$ip -> $mac\n"; }; ($ip,$mac) = $session->getnext($vars);
    SNMP is just one of those modules that have a steep learning curve. It's also extremely difficult to get it to work properly, at least for me.

    As for interfaces, I don't think that SNMP will get that for you. Instead, try SNMP::Info. Here's an example using SNMP::Info::Layer3::NetSNMP:

    #!/usr/bin/perl use strict; use warnings; use SNMP::Info; $SNMP::verbose = 1; $SNMP::debugging = 1; my $netsnmp = new SNMP::Info( AutoSpecify => 1, Debug => 1, DestHost => 'myrouter', Community => 'public', Version => 3 ) or die "Can't connect to DestHost.\n"; my $class = $netsnmp->class(); print "SNMP::Info determined this device to fall under subclass: $class\n";
Re: SNMP Perl Module methods not returning any values...
by pgwiz1 (Initiate) on Sep 02, 2010 at 14:43 UTC
    I FINALLY figured out my issue...in my session creation, I had to specify Version => 1 for it to work. Wanted to post back here incase others had a similar issue!