SNMP4J CommandResponder未接收到V3陷阱

时间:2019-01-05 01:35:49

标签: snmp snmp4j snmp-trap

我正在尝试基于Internet上的示例实现SNMP陷阱侦听器。我能够使它工作。 我的问题是V3陷阱消息没有被捕获。 我知道V3具有身份验证,因此我可以肯定问题可能与此有关,但是发送陷阱的路由器配置为不使用auth。

  

snmp服务器主机192.168.199.190版本3 noauth testuser   mac-notification snmp

这是我的相关代码:

public class TrapHandler implements CommandResponder {

    private static final String _V3_USERNAME = "testuser";
    private static final String _V3_AUTHENTICATION_PASSPHRASE = "idontKnowThis";

    private static final String _V3_PRIVACY_PASSPHRASE = "dontKnowThisEither";

    @Override
    public void processPdu(CommandResponderEvent crEvent) {

        PDU pdu = crEvent.getPDU();
        System.out.println("THE TYPE OF THE PDU: " + pdu.getType());
        if (pdu.getType() == PDU.V1TRAP) {

            PDUv1 pduV1 = (PDUv1) pdu;
            System.out.println("");
            System.out.println("===== NEW SNMP 1 TRAP RECEIVED ====");
            System.out.println("agentAddr " + pduV1.getAgentAddress().toString());
            System.out.println("enterprise " + pduV1.getEnterprise().toString());
            System.out.println("timeStam" + String.valueOf(pduV1.getTimestamp()));
            System.out.println("genericTrap" + String.valueOf(pduV1.getGenericTrap()));
            System.out.println("specificTrap " + String.valueOf(pduV1.getSpecificTrap()));
            System.out.println("snmpVersion " + String.valueOf(PDU.V1TRAP));
            System.out.println("communityString " + new String(crEvent.getSecurityName()));

        } else if (pdu.getType() == PDU.TRAP) {
            System.out.println("");
            System.out.println("===== NEW SNMP 2/3 TRAP RECEIVED ====");

            System.out.println("errorStatus " + String.valueOf(pdu.getErrorStatus()));
            System.out.println("errorIndex " + String.valueOf(pdu.getErrorIndex()));
            System.out.println("requestID " + String.valueOf(pdu.getRequestID()));
            System.out.println("snmpVersion " + String.valueOf(PDU.TRAP));
            System.out.println("communityString " + new String(crEvent.getSecurityName()));

        } else {
            System.out.println("Received a strange type of PDU " + pdu.getType());
        }

        Vector<? extends VariableBinding> varBinds = pdu.getVariableBindings();
        if (varBinds != null && !varBinds.isEmpty()) {
            Iterator<? extends VariableBinding> varIter = varBinds.iterator();

            StringBuilder resultset = new StringBuilder();
            resultset.append("-----");
            while (varIter.hasNext()) {
                VariableBinding vb = varIter.next();

                String syntaxstr = vb.getVariable().getSyntaxString();
                int syntax = vb.getVariable().getSyntax();
                System.out.println("OID: " + vb.getOid());
                System.out.println("Value: " + vb.getVariable());
                System.out.println("syntaxstring: " + syntaxstr);
                System.out.println("syntax: " + syntax);
                System.out.println("------");
            }

        }
        System.out.println("==== TRAP END ===");
        System.out.println("");
    }

    public synchronized void listen(TransportIpAddress address) throws IOException {
        AbstractTransportMapping transport;
        if (address instanceof TcpAddress) {
            transport = new DefaultTcpTransportMapping((TcpAddress) address);
        } else {
            transport = new DefaultUdpTransportMapping((UdpAddress) address);
        }

        ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
        MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); // maybe the issue is here?
        usm.setEngineDiscoveryEnabled(true);

        // add message processing models
        mtDispatcher.addMessageProcessingModel(new MPv1());
        mtDispatcher.addMessageProcessingModel(new MPv2c());
        mtDispatcher.addMessageProcessingModel(new MPv3(usm));

        // add all security protocols
        SecurityProtocols.getInstance().addDefaultProtocols();
        // SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES128());

        SecurityModels.getInstance().addSecurityModel(usm);

        // Create Target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));

        Snmp snmp = new Snmp(mtDispatcher, transport);
        UsmUser usmUser = new UsmUser(new OctetString("newUser"), AuthSHA.ID,
                new OctetString(_V3_AUTHENTICATION_PASSPHRASE), PrivAES128.ID, new OctetString(_V3_PRIVACY_PASSPHRASE));
        snmp.getUSM().addUser(new OctetString(_V3_USERNAME), usmUser); // or here
        snmp.addCommandResponder(this);

        transport.listen();
        System.out.println("Listening on " + address);

        try {
            this.wait();
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

V3有点复杂,因为它需要注册用户 就我而言,我有一个没有凭证的用户 所以我不得不更改以下内容

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

USM usm = new USM(SecurityProtocols.getInstance(), new OctetString("Receiver"), 0);

UsmUser定义正确

希望它对某人有帮助