#StackBounty: #java #snmp4j SNMP4j with snmpv3 response and error is null

Bounty: 50

I’m using snmp4j 3.4.2 inside my java application (full code below)

I’m trying to execute a snmpget with snmpv3, security DES and auth MD5 and custom OID (python script, which is executed by snmp’s extend funtionality). To create better understanding I used SnmpConstants.sysUpTime in the example below.

The SNMP resource has this user configured:

defSecurityName demo
defSecurityLevel authPriv
defAuthType MD5
defPrivType DES
defAuthPassphrase pass
defPrivPassphrase pass

I’m already using this user and resource to successfully perform the snmpget with python (pysnmp) and bash (snmpget), so I can definitely tell that my setup works and the java code is the problem.

I have two java classes (Listener.java and ServerStatusHelper.java)

Listener.java contains main and calls the snmpGet inside ServerStatusHelper.java, other code of Listener is excluded as its not neccessary.

import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;

public class Listener {

    public static void main(String[] args) {

        ServerStatusHelper agent = new ServerStatusHelper("host.tld", "udp", 161, "demo", "demo",
                "pass", "pass", new AuthMD5(), new PrivDES(), true);

        try {
            ResponseEvent response = agent.snmpGetOperation(SnmpConstants.sysUpTime);
            if (response != null) {
                        "response null - error: "+ response.getError() +
                        "peerAddress: " + response.getPeerAddress() +
                        "source: " + response.getSource().toString() +
                        "request: " + response.getRequest());

        } catch (

        IOException e) {




import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthGeneric;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyGeneric;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class ServerStatusHelper {

    private Address nmsIP;
    private String user;
    private String securityName;
    private String privacyPassword;
    private String authorizationPassword;
    private AuthGeneric authProtocol;
    private PrivacyGeneric privacyProtocol;
    private String protocol;
    private boolean encryption;

    private long timeOut = 1000;
    private int noOfRetries = 10;

    private Snmp snmp;
    private UserTarget target;
    private CommunityTarget v1target;
    ServerStatusHelper(String ip, String protocol, int snmpPort, String username, String securityName,
            String privacyPassword, String authPassowrd, AuthGeneric authProtocol, PrivacyGeneric privacyProtocol,
            boolean encryption) {

        nmsIP = GenericAddress.parse(protocol + ":" + ip + "/" + snmpPort);
        System.out.println("NMS IP set : " + nmsIP.toString());

        this.protocol = protocol;
        this.user = username;
        this.securityName = securityName;
        this.privacyPassword = privacyPassword;
        this.authorizationPassword = authPassowrd;
        this.authProtocol = authProtocol;
        this.privacyProtocol = privacyProtocol;
        this.encryption = encryption;
        SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthMD5());
        SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());


    public void startAgent() throws IOException {
        if (snmp == null) {

            TransportMapping<? extends TransportIpAddress> transport = null;

            if (protocol.equalsIgnoreCase("udp")) {
                System.out.println("UDP Protocol selected.");
                transport = new DefaultUdpTransportMapping();
            } else {
                System.out.println("TCP Protocol selected.");
                transport = new DefaultTcpTransportMapping();

            snmp = new Snmp(transport);

            USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);


            snmp.getUSM().addUser(new OctetString(user),
                    new UsmUser(new OctetString(securityName), authProtocol.getID(),
                            new OctetString(authorizationPassword), privacyProtocol.getID(),
                            new OctetString(privacyPassword)));

            if (encryption)
                target = createUserTarget();
                v1target = createUserTargetWithoutEncryption();


    public ResponseEvent snmpSetOperation(VariableBinding[] vars) throws IOException {
        PDU setPdu = new ScopedPDU();
        for (VariableBinding variableBinding : vars) {
        return snmp.send(setPdu, target);

    public ResponseEvent snmpGetOperation(OID oid) throws IOException {

        if (encryption) {
            PDU getPdu = new ScopedPDU();
            getPdu.add(new VariableBinding(oid));
            return snmp.get(getPdu, target);

        } else {
            PDU getPdu = new PDU();
            getPdu.add(new VariableBinding(oid));
            return snmp.get(getPdu, v1target);



    private UserTarget createUserTarget() {
        UserTarget target = new UserTarget();
        target.setSecurityName(new OctetString(securityName));
        return target;

    private CommunityTarget createUserTargetWithoutEncryption() {
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        return target;

    public long getTimeOut() {
        return timeOut;

    public void setTimeOut(long timeOut) {
        this.timeOut = timeOut;

    public int getNoOfRetries() {
        return noOfRetries;

    public void setNoOfRetries(int noOfRetries) {
        this.noOfRetries = noOfRetries;

The execution of the program exits with

NMS IP set : **IPREMOVED**/161
UDP Protocol selected.
response null - error: nullpeerAddress: **IPREMOVED**/161source: org.snmp4j.Snmp@e580929 request: GET[{contextEngineID=80:00:1f:88:80:5e:2e:49:07:2f:68:44:57:00:00:00:00, contextName=}, requestID=588252045, errorStatus=0, errorIndex=0, VBS[ = Null]]

Anyone has an idea what I’m doing wrong?


From the servers syslog I can see, that the request arrives at the resource:

Jul 31 11:52:46 loadbalancer snmpd[1219]: Connection from UDP: [IP REMOVED]:54734->[IP REMOVED]:161
Jul 31 11:52:46 loadbalancer snmpd[1219]: Connection from UDP: [IP REMOVED]:54734->[IP REMOVED]:161

Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.