snmp v3 with snmp4j unable to authenticate

23 Views Asked by At

snmp4j with snmp v2 working perfectly but with snmp v3, i'm getting this error:

org.snmp4j.MessageException: Message processing model 3 returned error: Unsupported security level

even though in mib browser with same credential it work perfectly

my code:

  String targetIP = "192.168.100.14";
        String community = "public";
        int port = 161;
        int retries = 5;
        int timeout = 5000;
        
        TransportMapping<? extends Address> transport;
        Snmp snmp = null;
        try
        {
            transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
         // transport.listen();
            snmp.listen();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        snmp.getUSM().addUser(new OctetString("adnan"),
                new UsmUser(new OctetString("adnan"),
                        AuthMD5.ID,
                        new OctetString("testtest"),
                        PrivDES.ID,
                        new OctetString("testtest")));
        PDU pdu = new ScopedPDU();
        pdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.15.1.2.2.1.3.17.128.0.31.136.128.254.166.152.44.147.94.237.101.0.0.0.0.5.97.100.110.97.110")));
        pdu.setType(PDU.GET);
        UserTarget target = new UserTarget();
        Address targetAddress = GenericAddress.parse(String.format("udp:%s/%s", targetIP, port));
        target.setAddress(targetAddress);
        target.setRetries(retries);
        target.setTimeout(timeout);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
        target.setSecurityName(new OctetString("adnan"));
     target.setSecurityModel(SecurityModel.SECURITY_MODEL_USM);
        ResponseEvent response;
        try
        {
            response = snmp.send(pdu, target);
            // extract the response PDU (could be null if timed out)
            PDU responsePDU = response.getResponse();
            // extract the address used by the agent to send the response:
            if(responsePDU == null)
            {
                System.out.println("ERROR: table OID [.1.3.6.1.6.3.15.1.2.2.1.3.17.128.0.31.136.128.254.166.152.44.147.94.237.101.0.0.0.0.5.97.100.110.97.110] due: "+response.getError() );
            }
            else
            {
                Address peerAddress = response.getPeerAddress();
                System.out.println("pdu: "+responsePDU.toString());
                System.out.println("Address: "+peerAddress.toString());
                System.out.println(responsePDU.get(0).getVariable().toString());
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                snmp.close();
            } catch (IOException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }

mib browser credential:

enter image description here

0

There are 0 best solutions below