1

I am trying to write a Perl script to do an SNMP get. It should work like the following command:

snmpget -v 3 -l authNoPriv -a MD5 -u V3User -A V3Password 10.0.1.203 sysUpTime.0

Returns:

SNMPv2-MIB::sysUpTime.0 = Timeticks: (492505406) 57 days, 0:04:14.06

But my Perl script returns the following:

ERROR: Received usmStatsUnknownUserNames.0 Report-PDU with value 1 during synchronization.

Last but not least, here is the Perl script:

use strict;
use warnings;

use Net::SNMP;

my $desc = 'sysUpTime.0';

my ($session, $error) = Net::SNMP->session(
   -hostname     => '10.0.1.202',
   -version      => 'snmpv3',
   -username     => 'V3User',
   -authprotocol => 'md5',
   -authpassword => 'V3Password'
);

if (!defined($session)) {
      printf("ERROR: %s.\n",  $error);
      exit 1;
}
my $response = $session->get_request($desc);
my %pdesc = %{$response};

my $err = $session->error;
if ($err){
   return 1;
}
print %pdesc;
exit 0;

I called the Perl script and snmpget on the same (Linux) machine. What could be causing this and how can I fix it?

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
Korbi
  • 714
  • 1
  • 9
  • 18
  • 1
    You can check this similar question:[SNMP V3 and Perl][1] [1]: http://stackoverflow.com/questions/25374612/snmp-v3-and-perl – tal.tzf Feb 01 '15 at 12:35

2 Answers2

3

As PrgmError points out, you're using a different IP address in your Perl script than in your snmpget command; I would double check that. The particular error you're getting indicates that your username is wrong; if the IP mismatch was simply a typo in your question, I would double check the username next.

A few other points about your Perl script:

Use die

You should use die instead of printf and exit since die will print the line number where it was invoked. This will make debugging your script much easier if there are multiple places it could fail:

die "Error: $error" if not defined $session;

will print something like

Error: foo bar at foo.pl line 17.

Also, using return inside an if statement doesn't make any sense; I think you meant to use

if ($err) {
    exit 1;
}

but you should die with the specific error message you get instead of silently failing:

die $err if $err;

Fix arguments to get_request

Your invocation of the get_request method looks wrong. According to the docs, you should be calling it like this:

my $response = $session->get_request(-varbindlist => [ $oid ]);

Note that Net::SNMP only works with numeric OIDs, so you'll have to change sysUpTime.0 to 1.3.6.1.2.1.1.3.0.

Community
  • 1
  • 1
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
1

Looking at your script I noticed that hostname value has 10.0.1.202

but the snmpget command you're using has 10.0.1.203

wrong IP by any chance?

PrgmError
  • 113
  • 1
  • 2
  • 7