Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

snmpwalk problem

by GS (Acolyte)
on Jun 20, 2005 at 01:27 UTC ( [id://468211]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,
I am trying to do a snmpwalk for interface descriptions on a cisco device using SNMP module, it looks there is no walk procedure available, I am trying the following and it walks through the whole MIBs instead of the branch just for interfaces.
Below is the script:
appreciate help, thanks!
use SNMP; $sess = new SNMP::Session(DestHost => cisco_router, Community => publ +ic); $vb = new SNMP::Varbind([.1.3.6.1.2.1.2.2.1.2]); do { $val = $sess->getnext($vb); print "@{$vb}\n"; } until ($sess->{ErrorNum});

Replies are listed 'Best First'.
Re: snmpwalk problem
by davidrw (Prior) on Jun 20, 2005 at 01:36 UTC
    I can't comment directly about your code, but i see that SNMP::Info can provide "Interface Information" and that SNMP::Util actually has a walk() method for doing a snmpwalk ... are either of these better suited for your needs?
      David, SNMP::Util requires ucd-snmp to be installed, and I am working in a production environment where as a user I am not able to install any packages at this point. regards.
Re: snmpwalk problem
by monarch (Priest) on Jun 20, 2005 at 07:07 UTC
    I always test the returned varbind to ensure the OID hasn't incremented past the branch that I want. (getnext always gets the next OID regardless of what the initial OID was). (OID = object identifier, it's the .1.3.6.1... thing)

    my $baseoid = ".1.3.6.1.2.1.2.2"; # ifTable my $thisoid = $baseoid; while ( 1 ) { $val = $sess->getnext($thisoid); last if ( $thisoid !~ m/^$baseoid\./ ); last if ( $sess->{ErrorNum} ); print( "@{$thisoid}\n" ); }

    Update: I always use Net::SNMP, too. Why? Because it's pure Perl, unlike SNMP which relies on the UCD tools. The latest Net::SNMP, however, does some tricky SNMPv3 stuff that requires a recent version of Perl, making it not that useful for running on old installations of 5.005.. in which case getting an older copy of Net::SNMP isn't a bad idea.

      Monarch,
      Tried the following and it didn't like the OID, see the output below.
      rgds.
      my $baseoid = ".1.3.6.1.2.1.2.2"; # ifTable my $thisoid = $baseoid; while ( 1 ) { $val = $sess->getnext($thisoid); print "$val \n"; last if ( $thisoid !~ m/^$baseoid\./ ); last if ( $sess->{ErrorNum} ); print( "@{$thisoid}\n" ); Cisco Internetwork Operating System Software IOS (tm) 3600 Software (C3640-JS-M), Version 12.1(5)T8, RELEASE SOFTW +ARE (fc1) TAC Support: http://www.cisco.com/cgi-bin/ibld/view.pl?i=support Copyright (c) 1986-2001 by cisco Systems, Inc. Compiled Tue
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: snmpwalk problem
by decnartne (Beadle) on Jun 21, 2005 at 14:52 UTC
    I use the following in a script I run to get a list of interfaces from Cisco... (perhaps the leading "." on your varbind is the culprit? - or not...)
    ... use strict; use Net::SNMP; ... sub get_if_ids { my ( $hostname ) = @_; my %ifs; my $ifEntry = "1.3.6.1.2.1.2.2.1.2"; my ( $session, $error ) = Net::SNMP->session( -hostname => $hostname, -version => 1, -community => "public" ); if ( !defined( $session ) ) { print "ERROR: SNMP - $error\n"; return undef; } my $result = $session->get_next_request( -varbindlist => [$ifEntry +] ); while ( defined( $result ) ) { foreach my $key ( keys %$result ) { if ( $key !~ /^$ifEntry/ ) { $result = undef; last; } # process $result... # Update: $key is 1.3.6.1.2.1.2.2.1.2.n for the nth inte +rface # $result->{$key} is interface name $result = $session->get_next_request( -varbindlist => [$ke +y] ); } } $session->close(); return \%ifs; }

    decnartne ~ entranced

      Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2025-04-29 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.