在第一次发送后几次无法接收SNMPv3陷阱。 [SNMP4J的Java]

时间:2017-03-13 10:22:40

标签: java snmp sender receiver snmp4j

我在过去的几天里一直在努力使用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();
  • 我正在使用snmp4j-1.1依赖项。我不知道为什么它会忽略在第一次发送后大约2分钟出现的陷阱。

    非常感谢朋友们的帮助

1 个答案:

答案 0 :(得分:0)

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

有一个很好的代码日: - )