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";

In reply to Re: SNMP Perl Module methods not returning any values... by Khen1950fx
in thread SNMP Perl Module methods not returning any values... by pgwiz1

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.