snmp4j - 使用NoAuthNoPriv的v3 USM

时间:2015-09-28 21:33:53

标签: snmp snmp4j

在snmp4j.org上尝试Frank的简单snmpv3示例,以下代码继续返回一个报告,其OID为1.3.6.1.6.3.15.1.1.3.0,这意味着UnknownUserName ..

首先,这是正确连接并返回OIDS表的示例snmpwalk。

snmpwalk -v3 -n DefaultContextName -l noAuthNoPriv -u "public-1" x.x.x.x:161 1.3.6.1.4.1.x

以下snmp4j代码

    public static void main(String[] args){

        try {

            System.out.println("DEBUG:MW>Initiated main...");

            Address targetAddress = GenericAddress.parse("udp:x.x.x.x/161");
            TransportMapping transport = new DefaultUdpTransportMapping();
            Snmp snmp = new Snmp(transport);
            USM usm = new USM(SecurityProtocols.getInstance(),
                    new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
            transport.listen();

            // add user to the USM
//            snmp.getUSM().addUser(new OctetString("public-1"),
//                    new UsmUser(new OctetString("DefaultContextName"),
//                            null,
//                            null,
//                            null,
//                            null));
             snmp.getUSM().addUser(new OctetString("MD5DES"),
             new UsmUser(new OctetString("MD5DES"),
                         AuthMD5.ID,
                         new OctetString("readuser"),//MD5DESUserAuthPassword
                         PrivDES.ID,
                         new OctetString("readuser2")));//MD5DESUserPrivPassword

            // create the target
            UserTarget target = new UserTarget();
            target.setAddress(targetAddress);
            target.setRetries(1);
            target.setTimeout(10000);
            target.setVersion(SnmpConstants.version3);
            target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
            target.setSecurityName(new OctetString("MD5DES"));

            // create the PDU
            PDU pdu = new ScopedPDU();
            pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.x")));
            pdu.setType(PDU.GETNEXT);

            // send the PDU
            ResponseEvent 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:
            Address peerAddress = response.getPeerAddress();

            System.out.println("DEBUG:MW: REVISED PDU toString>"+responsePDU.toString());


        }catch(Exception e){
            System.err.println(e.getMessage());
            e.printStackTrace();
        }

上面你可能会注意到一个已注释掉的USM对象正确拥有'public-1'用户名。这总是会导致超时,因此responsePDU为null。

非常感谢提前......和其他人一样 - 我正处于紧张状态,真的需要一些帮助!

0 个答案:

没有答案