Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Perl Net::SNMP returns 1 instead of the desired outcome

by vlad3848 (Acolyte)
on Oct 25, 2013 at 07:34 UTC ( #1059604=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am writing a script that will check date/time on a network device. I am using Net::SNMP module to walk the OID for the current time on the device and then will compare it against current time on a server..However, whenever i walk the switch for the time, the return value comes back as 1 instead of the actual value. I can manually walk the switch, or use system call to invoke snmpwalk, but since i need to do this on a couple of thousand of devices i am really trying to stick with SNMP module. Any ideas of what i am doing wrong?

# /usr/bin/perl use warnings; use strict; use Net::SNMP; use Data::Dumper; my $oid= '1.3.6.1.4.1.1588.2.1.1.1.1.1'; my $host="host1.testdoman.com"; my $community='test'; my ($session, $error) = Net::SNMP->session( -hostname => $host, -community =>$community, -nonblocking => 1, ); my $result = $session->get_request(-varbindlist => [ $oid ],); if (!defined $result) { $error = $session->error(); print "GET_REQUEST ERROR: $error\n"; $session->close(); exit(1); } print Dumper $result; $session->close; exit(0);

Just to illustrate what output i am expecting. The following code invokes snmpwalk utility and produces the desired output:

# /usr/bin/perl use warnings; use strict; my $community='test'; my $host= "host1.testdoman.com"; my $Time = `snmpwalk -v1 -c $community $host 1.3.6.1.4.1.1588.2.1.1.1 +.1.1`; print $Time;

SNMPv2-SMI::enterprises.1588.2.1.1.1.1.1.0 = STRING: "Fri Oct 25 07:23:33 2013 "

However, when i try to use the Net:SNMP module it produces output $VAR1 = 1;

Any help would be appreciated!!

Thanks in advance

Replies are listed 'Best First'.
Re: Perl Net::SNMP returns 1 instead of the desired outcome
by kcott (Bishop) on Oct 25, 2013 at 08:03 UTC
Re: Perl Net::SNMP returns 1 instead of the desired outcome
by VinsWorldcom (Prior) on Oct 25, 2013 at 13:05 UTC

    From the perldoc of Net::SNMP:

    get_request() - send a SNMP get-request to the remote agent $result = $session->get_request( [-callback => sub {},] # non-blocking [-delay => $seconds,] # non-blocking [-contextengineid => $engine_id,] # v3 [-contextname => $name,] # v3 -varbindlist => \@oids, ); [...] A reference to a hash is returned in blocking mode which contains the contents of the VarBindList. In non-blocking mode, a true value is returned when no error has occurred. In either mode, the undefined value is returned when an error has occurred. The "error()" method may be used to determine the cause of the failure.

    I see you're using 'nonblocking' mode in your call to session() so you're getting a "TRUE" value returned as per the documented operation. Try removing the '-nonblocking => 1' line from the call to session() and see what happens.

Re: Perl Net::SNMP returns 1 instead of the desired outcome
by Khen1950fx (Canon) on Oct 25, 2013 at 19:18 UTC
    I used snmpv1, blocking, and printf instead of print; also, I added DEBUG_ALL to make it more interesting:
    # /usr/bin/perl use strict; use warnings; use Net::SNMP qw(:snmp DEBUG_ALL); my $OID = '1.3.6.1.2.1.1.3.0'; my ( $session, $error ) = Net::SNMP->session( -hostname => shift || 'localhost', -community => shift || 'public', -version => 'snmpv1', -debug => DEBUG_ALL, ); my $result = $session->get_request( -varbindlist => [$OID], ); if ( !defined $result ) { $error = $session->error(); printf "GET_REQUEST ERROR: %s: %s\n", $session->error(); $session->close(); exit(1); } printf "The TimeTicks for host '%s' is %s\n", $session->hostname(), $result->{$OID}, "\n"; $session->close; exit(0);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1059604]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-08-15 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (78 votes). Check out past polls.

    Notices?