Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Net::SNMP get_request problem

by chrism01 (Friar)
on Sep 29, 2005 at 01:13 UTC ( [id://495954]=perlquestion: print w/replies, xml ) Need Help??

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

Monks
I'm trying to cvt a cmd line snmp call like this:

/usr/bin/snmpget -Oq 1.2.3.4 passwd interfaces.ifTable.ifEntry.ifOutOctets.6

to a Perl Net::SNMP cmd as part of a Perl script.
I tried the following, based on the example from the relevant CPAN module page:
#!/usr/bin/perl -w use strict; use Net::SNMP; my ($session, $error) = Net::SNMP->session( -hostname => shift || '1.2.3.4', -community => shift || 'passwd', -port => shift || 6 ); if (!defined($session)) { printf("Session ERROR: %s.\n", $error); exit 1; } #my $octets_out = '1.3.6.1.2.1.1.3.0'; my $octets_out = 'interfaces.ifTable.ifEntry.ifOutOctets'; my $result = $session->get_request( -varbindlist => [$octets_out] ); if (!defined($result)) { printf("get_req ERROR: %s.\n", $session->error); $session->close; exit 1; } printf("octets_out for host '%s' is %s\n", $session->hostname, $result->{$octets_out} ); $session->close;
and got the following result:

get_req ERROR: Expected OBJECT IDENTIFIER in dotted notation.

Can anybody help me out, bearing in mind that I've never used SNMP before?
(In both examples I've obscured the ip address + communitystring)
Thx
Chris

Replies are listed 'Best First'.
Re: Net::SNMP get_request problem
by pg (Canon) on Sep 29, 2005 at 02:43 UTC

    If you open up the source code (the code is in Net::SNMP::PDU), you will see that the module is expecting the input to match

    /^\.?\d+\.\d+(?:\.\d+)*/

    And the format that you commented out was just fine.

    use strict; my $a = '1.3.6.1.2.1.1.3.0'; if ($a =~ /^\.?\d+\.\d+(?:\.\d+)*/) { print "match\n"; }
Re: Net::SNMP get_request problem
by chrism01 (Friar) on Sep 29, 2005 at 03:59 UTC
    Actually, I should have pointed out that the line with the commented out IP address was from the CPAN example.
    (maybe I should have deleted it :) )
    The cmd line code I inherited uses the names format instead, and I don't know what the numbers would be.
    Is there any way of continuing to use the names, as it's a darn sight more informative/maintainable?
    If not, how do I find out the numbers?
    Thx again
    Chris
      one quick way to find out the numbers (OIDs) of the names, is to use the snmpwalk or snmpget command with the option -On.
      example:
      insaniac][musashi: ~ : snmpget -On -v1 -cpublic localhost interfaces.i +fTable.ifEntry.ifOutOctets.1 .1.3.6.1.2.1.2.2.1.16.1 = Counter32: 4093056087 insaniac][musashi: ~ : snmpget -v1 -cpublic localhost interfaces.ifTab +le.ifEntry.ifOutOctets.1 IF-MIB::ifOutOctets.1 = Counter32: 4093056087

      to ask a question is a moment of shame
      to remain ignorant is a lifelong shame

Re: Net::SNMP get_request problem
by spiritway (Vicar) on Sep 29, 2005 at 01:59 UTC

    I've checked your code. It runs when I reverse your comments, as follows:

    my $octets_out = '1.3.6.1.2.1.1.3.0'; #my $octets_out = 'interfaces.ifTable.ifEntry.ifOutOctets';

    Not surprisingly, I got a message telling me that host 1.2.3.4 wasn't responding, but it certainly is a hopeful indication that you're on the right track.

    Could it be that your code (as I changed it) was working, but you thought it was hanging because it was timing out?

Re: Net::SNMP get_request problem
by insaniac (Friar) on Sep 30, 2005 at 08:57 UTC
    From my days with playing with Net::SNMP, I remember that I liked the get_next_request more than get_request and all my OIDs start with a dot.
    Some working code (which i seem to copy paste a lot):
    my ($s,$e)=Net::SNMP->session(-hostname=> $ip, -community =>$community +); if (!defined($s)) { printf("error: %s.\n", $e); exit 1; } $oid = ".1.3.6.1.4.1.9.9.42.1.2.1.1.2"; my @args = ( -varbindlist => [$oid]); while ( defined $s->get_next_request(@args) ) { $_ = ( keys(%{ $s->var_bind_list }) )[0]; if (!oid_base_match($oid, $_)) { last; } printf("%s => %s", $_, $s->var_bind_list->{$_}); my @n = split /\./, $_; print " (index: ",$n[$#n],")\n"; @args = (-varbindlist => [$_]); } # Let the user know about any errors if ($s->error() ne '') { print $s->error(),"\n"; }
    Try to see if you understand this small example (read: perldoc it);-)

    cheers

    PS: I think *your* problem is just that you have to start your OID with a DOT (like the error message says). Nothing more...

    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found