第一次发送后几次无法接收 SNMPv3 陷阱。 [snmp4j-Java]

Unable to receive SNMPv3 Traps few times after the first Send. [snmp4j-Java]

过去几天我一直在努力使用 Auth 和 priv 短语发送 SNMPV3 陷阱。问题是:在我收到第一个带有 Auth-Priv 的 Trap SNMPv3 后,有时(大约 2 分钟)我无法收到陷阱。接收者就像无视每一个即将到来的陷阱

这是我的代码:

Receiver.java

    try {
        // set udpAdress and transportMapping
        final UdpAddress udpAddress = new UdpAddress(162);
        final TransportMapping transportMapping = new          DefaultUdpTransportMapping(udpAddress);

        // Protocole de sécurité +usm +snmp
        this.snmp = new Snmp(new MessageDispatcherImpl(), transportMapping);
        SecurityProtocols.getInstance().addDefaultProtocols();
        final USM usm = new USM(SecurityProtocols.getInstance(),
            new OctetString(MPv3.createLocalEngineID(new OctetString())),
            0);
        SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());
        usm.setEngineDiscoveryEnabled(true);

        // Add the MPv
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(usm));

        // Security model
        SecurityModels.getInstance().addSecurityModel(usm);

        // Add user 
        snmp.getUSM().addUser(new OctetString("v3AuthSHAPrivDESSecName"),
            new UsmUser(new OctetString("v3AuthSHAPrivDESSecName"), AuthMD5.ID,
                new OctetString("v3AuthSHAPrivDESAuthPassword"), PrivDES.ID,
                new OctetString("v3AuthSHAPrivDESPrivPassword")));

        // Launch of the listener
        this.snmp.addCommandResponder(this);
        snmp.listen();`

Sender.java

try {
        // set udpAdress and transportMapping
        Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
        TransportMapping transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);

        // Protocole de sécurité +usm +snmp
        SecurityProtocols.getInstance().addDefaultProtocols();
        final USM usm = new USM(SecurityProtocols.getInstance(),
            new OctetString(MPv3.createLocalEngineID(new OctetString())),
            0);

        SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());
        SecurityModels.getInstance().addSecurityModel(usm);

        transport.listen();

        // Ajout d'un user avec les paramètres de sécurité
        snmp.getUSM().addUser(new OctetString("v3AuthSHAPrivDESSecName"),
            new UsmUser(new OctetString("v3AuthSHAPrivDESSecName"), AuthMD5.ID,
                new OctetString("v3AuthSHAPrivDESAuthPassword"), PrivDES.ID,
                new OctetString("v3AuthSHAPrivDESPrivPassword")));

        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1111);
        target.setTimeout(11111500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
        target.setSecurityName(new OctetString("v3AuthSHAPrivDESSecName"));

        // Create PDU 1 for V3
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(PDU.TRAP);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID(trapOid), new OctetString("V33333")));
        snmp.send(pdu, target);

        System.out.println("Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);

        snmp.addCommandResponder(new CommandResponder() {
            public void processPdu(CommandResponderEvent arg0) {
                System.out.println(arg0);
            }
        });
        snmp.close();

我找到答案了! :D 只需将依赖项更改为 snmp4j-2.* 即可,现在我使用了 snmp4j-2.5.0。它工作完美,如果你想使用上面的代码,它工作完美。

祝你代码日愉快:-)