SNMP4j引擎ID发现

时间:2018-06-16 16:28:35

标签: snmp4j

我正在使用snmp4j 2.6.2库并尝试通过添加v3 authpriv用户使用mibbrowser发现上下文引擎ID。我能够添加用户,但我在mib浏览器上收到的响应ID是0,而不是正确的请求ID。

public void loadTrapServiceProperties() throws Exception{
        try {
            Address targetAddress = GenericAddress.parse("udp:" + "127.0.0.1"
            + "/" +8162);


            UdpAddress udpAddress = new UdpAddress(InetAddress.getByName("127.0.0.1"), 8161); 
            TransportMapping transport = new DefaultUdpTransportMapping(udpAddress);

            ThreadPool threadPool =   
                    ThreadPool.create("DispatcherPool", 2);   
                MessageDispatcher disp =   
                    new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());



            String trapEnginePrefix = "80:00:01:2F:01";
            String floatingIP = "xx.x.xx.xx";
            setEngineId(trapEnginePrefix, floatingIP);
            byte[] eg = engineID.toString().getBytes();




            //USM usm = new USM();
            USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), new OctetString(
                    engineID.toString()), 0);
            UsmUser usmuser = new UsmUser(new OctetString("owuser"), AuthSHA.ID,
                    new OctetString("12345678"), PrivAES128.ID,
                    new OctetString("12345678"));


             disp.addMessageProcessingModel(new MPv3(usm));

            snmp = new Snmp(disp,transport);
            snmp.setLocalEngine(OctetString.fromHexString(engineID.toString()).toByteArray(),0,0);
            snmp.getUSM().addUser(new OctetString("owuser"),usmuser);
            snmp.addCommandResponder(this);
            transport.listen();
            System.out.println("Listening");




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

        } catch (Exception e) {
            System.out.println("Error in Sending Trap to (IP:Port)=> {} : {} which is {}");
            throw e;
        }
    }

    private String setEngineId(String trapEnginePrefix, String ip) throws Exception {
        StringBuilder engineIdStr = null;
        try {
            String floatingIP[] = ip.split("\\.");
            engineID = new StringBuilder(trapEnginePrefix)
                    .append(":")
                    .append(appendZeroToString(Integer.toHexString(Integer.parseInt(floatingIP[0])), 2))
                    .append(":")
                    .append(appendZeroToString(Integer.toHexString(Integer.parseInt(floatingIP[1])), 2))
                    .append(":")
                    .append(appendZeroToString(Integer.toHexString(Integer.parseInt(floatingIP[2])), 2))
                    .append(":")
                    .append(appendZeroToString(Integer.toHexString(Integer.parseInt(floatingIP[3])), 2));
            engineIdStr = engineID;
            engineIdStr = new StringBuilder("80:00:13:70:01:c0:a8:00:06:7b");
            engineID = engineIdStr;
            System.out.println("Engine ID Generated is {}"+engineIdStr);

        } catch (Exception e) {
            System.out.println("Error while generating Engine Id "+ e);
            throw  e;

        }
        return engineIdStr.toString();
    }

    // utility to prefix 0's to a number
    public static String appendZeroToInteger(int value, int length) {
        String valueString = String.valueOf(value);
        if (-1 == length) {
            return "";
        } else {
            int valueLength = valueString.length();
            for (int i = 0; i < (length - valueLength); i++) {
                valueString = "0" + valueString;
            }
            return valueString;
        }
    }

    // utility to prefix 0's to a string
        public static String appendZeroToString(String value, int length) {
            String valueString = value;
            if (-1 == length) {
                return "";
            } else {
                int valueLength = value.length();
                for (int i = 0; i < (length - valueLength); i++) {
                    valueString = "0" + valueString;
                }
                return valueString;
            }
        }

        public static void main(String [] arg) throws Exception {


            SNMPTest snmpTest = new SNMPTest();
            snmpTest.loadTrapServiceProperties();
        }

        @Override
        public void processPdu(CommandResponderEvent e) {

            PDU command = (PDU) e.getPDU(); 
            if (command != null) {   
              System.out.println(command.toString());   
              if ((command.getType() != PDU.TRAP) &&   
                  (command.getType() != PDU.V1TRAP) &&   
                  (command.getType() != PDU.REPORT) &&   
                  (command.getType() != PDU.RESPONSE)) {   
                command.setErrorIndex(0);   
                command.setErrorStatus(0);   
                command.setType(PDU.RESPONSE);

                e.getStateReference().setTransportMapping(e.getTransportMapping());



               e.getPduHandle().setTransactionID(command.getRequestID().toInt());
                StatusInformation statusInformation = new StatusInformation();  
                StateReference ref = e.getStateReference();   
                try {   
                  e.getMessageDispatcher().returnResponsePdu(e.   
                                                             getMessageProcessingModel(),   
                                                             e.getSecurityModel(),   
                                                             e.getSecurityName(),   
                                                             e.getSecurityLevel(),   
                                                             command,   
                                                             e.getMaxSizeResponsePDU(),   
                                                             ref,   
                                                             statusInformation);   
                }   
                catch (MessageException ex) {   
                  System.err.println("Error while sending response: "+ex.getMessage());   

                }   
              }   
            }
    }

” 请求 - 响应数据包

PDU: 30 3f 02 01 03 30 0e 02 01 04 02 03 00 ff ff 04 01 04 02 01 03 04 17 30 15 04 00 02 01 00 02 01 00 04 07 69 6e 69 74 69 61 6c 04 00 04 00 30 11 04 00 04 00 a0 0b 02 01 03 02 01 00 02 01 00 30 00 版本:3(v3) 消息ID:4 最大尺寸:65535 旗帜:4 安全模型:3 安全级别:NO_AUTH_NO_PRIV 权威引擎ID:null 引擎靴:0 发动机时间:0 用户名:初始 验证参数:null Priv参数:null ContextEngineID:null ContextName:null 命令:GET请求 请求ID:3 错误状态:0(无错误。) 错误索引:0 Varbinds:

PDU: 30 64 02 01 03 30 0e 02 01 04 02 03 00 ff ff 04 01 00 02 01 03 04 21 30 1f 04 0a 80 00 13 70 01 c0 a8 00 06 7b 02 01 00 02 01 00 04 07 69 6e 69 74 69 61 6c 04 00 04 00 30 2c 04 0a 80 00 13 70 01 c0 a8 00 06 7b 04 00 a8 1c 02 01 00 02 01 00 02 01 00 30 11 30 0f 06 0a 2b 06 01 06 03 0f 01 01 04 00 41 01 00 版本:3(v3) 消息ID:4 最大尺寸:65535 标志:0 安全模型:3 安全级别:NO_AUTH_NO_PRIV 权威引擎ID:0x 80 00 13 70 01 c0 a8 00 06 7b 引擎靴:0 发动机时间:0 用户名:初始 验证参数:null Priv参数:null ContextEngineID:0x 80 00 13 70 01 c0 a8 00 06 7b ContextName:null 命令:报告 请求ID:0 错误状态:0(无错误。) 错误索引:0 Varbinds: 对象ID:.1.3.6.1.6.3.15.1.1.4.0 柜台:0 “

0 个答案:

没有答案