snmp4jjava1.10迁移到2.8.6-snmpv3 set请求:如果密码短语错误,responseevent为null

ohfgkhjo  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(282)

我对snmp4j lib有问题。在我的项目中,我们使用了snmp4jv。1.10.2上一次,如果我们有许多线程有不同的请求(get、set、bulkwalk),我们就遇到了一个问题:一些snmpset会得到任何响应。我们决定迁移到snmp4jv的新版本。2.8.6我们遇到了一些新问题。
案例1:
如果securityname是错误的->我们将得到身份验证失败的响应
案例2:
如果authenticationpassphrase错误->responseevent==null
案例3:
如果privacypassphrase错误->responseevent==null
案例4:
如果authenticationpassphrase&privacypassphrase错误->responseevent==null(responsepdu==null)
如果我在snmp4j1.10.2下编译相同的代码,我总是得到一个响应,但是如果我在snmp4j2.8.6上构建它,我永远不会得到任何响应;它总是空的。这个问题只在版本snmp4jv2.2+中检测到(从版本1.10.2到2.1,我总是得到响应)。
我读过变更日志(https://www.snmp4j.org/changes.txt)和rfc3414(https://tools.ietf.org/html/rfc3414#section-3.2)并尝试不同的选择,但我没有解决问题。
我希望有人能帮我。谢谢

....
..
         else if (snmpVersion == SnmpConstants.version3) {

         Address targetAddress = new UdpAddress(InetAddress.getByName(componentAddress), port);
         UserTarget target = new UserTarget(); 
         target.setAddress(targetAddress);
         target.setRetries(new Long(deviceActionContext.getSnmpActionContext().getRetries()).intValue());
         target.setSecurityName(new OctetString(deviceActionContext.getSnmpActionContext().getSnmpv3WriteUser()));

         if (deviceActionContext.getSnmpActionContext().getTimeout() > 0) {
          target.setTimeout(new Long(deviceActionContext.getSnmpActionContext().getTimeout()).intValue());
         } else {
           target.setTimeout(-1);
         }
         target.setVersion(snmpVersion);

         TransportMapping transport = null;
         ScopedPDU pdu = new ScopedPDU();
         VariableBinding vb = new VariableBinding(new OID(snmpMib));
         vb.setVariable(newValue);
         pdu.add(vb);
         pdu.setType(PDU.SET);
         transport = new DefaultUdpTransportMapping();
         snmp = new Snmp(transport);
         ResponseEvent response = null;
         PDU responsePDU = null;
         transport.listen();

         setSnmpPrincipals(snmp, snmpV3SecurityLevel, target, snmpV3HashOid, snmpV3EncryptOid, snmpV3HashAlg, snmpV3Encryption, SnmpUserRights.Write);

         response = snmp.send(pdu, target);
         responsePDU = response.getResponse();

          if (responsePDU == null) {//Device did not send a response.
          String error = " Wert: " + snmpSetValue;
          throw new LANAccessException(2055, Constants.ERRORCODES.getProperty("2055") + error, componentAddress, false);
        }

        if (responsePDU.getType() == PDU.REPORT){//Authentification faliure

           throw new LANAccessException(7002, Constants.ERRORCODES.getProperty("7002") + "SNMP V" + 
          deviceActionContext.getSnmpActionContext().getSnmpVersion(), componentAddress, true);
        }
..
..
    finally {
        try {
            transport.close();
          } catch (Exception ex) {
            // close nicht moeglich...
            logger.error("transport close has thrown exception: " + ex.getMessage());
          }
         try {
              snmp.close();
            } catch (Exception ex) {
              // close nicht moeglich...
              logger.error("snmp close has thrown exception: " + ex.getMessage());
         }
    }

设置NMP原则

private void setSnmpPrincipals(Snmp snmp, String snmpV3SecurityLevel, UserTarget target, OID snmpV3HashOid, OID snmpV3EncryptOid,
                String snmpV3HashAlg,String snmpV3Encryption, SnmpUserRights snmpUserRights) {

     switch(snmpUserRights) {

     case Read: 
            ...

     case Write: 

             if (snmpV3SecurityLevel.equalsIgnoreCase("AUTH_PRIV") == true) {

                 target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
                 snmp.getUSM().addUser(new OctetString(deviceActionContext.getSnmpActionContext().getSnmpv3WriteUser()),
                   new UsmUser(new OctetString(deviceActionContext.getSnmpActionContext().getSnmpv3WriteUser()),
                     snmpV3HashOid, new OctetString(deviceActionContext.getSnmpActionContext().getSnmpv3WriteAuthPassword()),
                     snmpV3EncryptOid, new OctetString(deviceActionContext.getSnmpActionContext().getSnmpv3WritePrivPassword())));

               } else if (...)

  }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题