SNMP getnext

The SNMP getnext operation is commonly referred to as "walking a MIB." The example source implements a command-line snmpwalk tool similar to that of Net-SNMP's snmpwalk:

    public class SnmpWalk implements PDUFactory {
    ...
    }

Our SnmpWalk class implements PDUFactory. This means we must implement a method with the following signature:

    public PDU createPDU(Target target) {
    ...
    }

The createPDU method is responsible for creating the proper PDU, either an SNMPv2c or SNMPv3 PDU. The type of PDU (the particular SNMP operation) is also configured as part of the version-specific PDU. This is accomplished by setting a member variable that is used when createPDU() is called:

      private int _pduType = PDU.GETNEXT;

The class has two different constructors:

    public SnmpWalk(String host, String oid){
    ...
    }
    public SnmpWalk(String host, String oid, String user, String authProtocol,
      String authPassphrase, String privProtocol, String privPassphrase) {
    ...
    }

The first constructor creates an SNMPv2c walk command. The arguments are the host on which an agent is running and the prefix of the OIDs we want to walk. The community string is hardcoded in the application, but ordinarily you will want to pass this as a constructor or expose a setter. The second constructor creates an SNMPv3 walk command. The first two arguments are the same as the first constructor, but the other arguments deal with setting up SNMPv3 security.

Each constructor sets the particular version of SNMP by using one of the following ...

Get Essential SNMP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.