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