O'Reilly logo

Essential SNMP by Kevin Schmidt, Douglas Mauro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Retrieving Multiple MIB Values

The syntax for snmpwalk is similar to the syntax for its cousin, snmpget. As discussed in Chapter 2, snmpwalk traverses a MIB starting with some object, continuously returning values until it gets to the end of that object’s branch. For example, the upcoming Perl script begins walking the .iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr object and provides a description of each Ethernet interface on the device it polls.

This new script is a minor modification of snmpget.pl. We turned the scalar $value into the array @values;[32] we need an array because we expect to get multiple values back. We also called the function snmpwalk instead of snmpget (syntactically, the two functions are the same):

#!/usr/local/bin/perl
#filename: /opt/local/perl_scripts/snmpwalk.pl
use SNMP_util;
$MIB1 = shift;
$HOST = shift;
($MIB1) && ($HOST) || die "Usage: $0 MIB_OID HOSTNAME";
(@values) = &snmpwalk("$HOST","$MIB1");
if (@values) { print "Results :$MIB1: :@values:\n"; }
else { warn "No response from host :$HOST:\n"; }

Here’s how to run the script:

$ /opt/local/perl_scripts/snmpwalk.pl .1.3.6.1.2.1.2.2.1.2 orarouter1

This command walks down the .iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifDescr object, returning information about the interfaces that are on the router. The results look something like this:

Results :.1.3.6.1.2.1.2.2.1.2: :1:Ethernet0 2:Serial0 3:Serial1:

The output depends on the interfaces on the host or router ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required