消息处理模型 3 返回错误:不支持的安全级别

问题描述 投票:0回答:0

这是我的 MultiThreadedTrapReceiver 类,它将接收请求陷阱:

 package org.example.snmp;

 import org.snmp4j.security.SecurityModels;
 import org.snmp4j.security.TSM;
 import java.io.IOException;
 import java.net.InetAddress;  
 import java.net.UnknownHostException;
 import java.util.Vector;
 import java.util.logging.Level;
 import java.util.logging.Logger;

 import org.snmp4j.*;
 import org.snmp4j.mp.*;
 import org.snmp4j.security.*;
 import org.snmp4j.smi.*;
 import org.snmp4j.transport.*;
 import org.snmp4j.util.*;

 public class MultiThreadedTrapReceiver implements CommandResponder {

      private Address address = GenericAddress.parse("0.0.0.0/162");
      private final int numDispatcherThreads = 2;
      private final OID authProtocol = AuthSHA.ID;
      private final OID privProtocol = PrivAES256.ID;
      private final OctetString securityName = new OctetString("SHADES");
      private final OctetString privPassphrase = new OctetString("SHADESUserPrivPassword");
      private final OctetString authPassphrase = new OctetString("SHADESUserAuthPassword");
      private static final OctetString localEngineId =
      new OctetString(MPv3.createLocalEngineID());


      public MultiThreadedTrapReceiver() {
           try {
             listen();
           } catch (IOException ex) {
             Logger.getLogger(MultiThreadedTrapReceiver.class.getName())
  .log(Level.SEVERE, null, ex);
      }
   }

   public synchronized void listen() throws IOException {
        AbstractTransportMapping transport;
        if (address instanceof TcpAddress) {
            transport = new DefaultTcpTransportMapping((TcpAddress) address);
        } else {
            transport = new DefaultUdpTransportMapping((UdpAddress) address);
    }
    ThreadPool threadPool =
            ThreadPool.create("DispatcherPool", numDispatcherThreads);
    MessageDispatcher mtDispatcher =
            new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

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

    SecurityModels.getInstance().addSecurityModel(new TSM(localEngineId, false));

    // add all security protocols
    SecurityProtocols securityProtocols = SecurityProtocols.getInstance();
    securityProtocols.addDefaultProtocols();
    securityProtocols.addAuthenticationProtocol(new AuthMD5());
    securityProtocols.addAuthenticationProtocol(new AuthSHA());
    securityProtocols.addPrivacyProtocol(new PrivDES());
    securityProtocols.addPrivacyProtocol(new Priv3DES());
    securityProtocols.addPrivacyProtocol(new PrivAES128());
    securityProtocols.addPrivacyProtocol(new PrivAES192());
    securityProtocols.addPrivacyProtocol(new PrivAES256());

    Snmp snmp = new Snmp(mtDispatcher, transport);
    USM usm = new USM(SecurityProtocols.getInstance(),  localEngineId, 0);
    usm.setEngineDiscoveryEnabled(true);
    SecurityModels.getInstance().addSecurityModel(usm);
    // Add the configured user to the USM
    addUsmUser(snmp);

    snmp.addCommandResponder(this);

    transport.listen();

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

    private void addUsmUser(Snmp snmp) {
    snmp.getUSM().addUser(securityName, new UsmUser(securityName,
            authProtocol,
            authPassphrase,
            privProtocol,
            privPassphrase));
     }

     @Override
    public void processPdu(CommandResponderEvent respEvnt) {
    System.out.println(respEvnt.getPDU());
    InetAddress pduAgentAddress = null;
    //System.out.println(respEvnt.getPDU() + " recieved;");
    //this.setPdu(respEvnt.getPDU());
    OctetString community = new OctetString(respEvnt.getSecurityName());
    System.out.println("community: " + community.toString());

    Address address = respEvnt.getPeerAddress();
    String hostName = address.toString().split("/")[0];
    int nPort = Integer.parseInt(address.toString().split("/")[1]);

    //handle the SNMP v1
    if (respEvnt.getPDU().getType() == PDU.TRAP) {
        try {
            pduAgentAddress = InetAddress.getByName(hostName);
        } catch (UnknownHostException ex) {
            Logger.getLogger(MultiThreadedTrapReceiver.class.getName())
   .log(Level.SEVERE, null,     ex);
        }
        assert pduAgentAddress != null;
        System.out.println("hostname: " + pduAgentAddress.getHostAddress() 
     + "; port: " + nPort);
    } else {
        try {
            pduAgentAddress = InetAddress.getByName(hostName);
        } catch (UnknownHostException ex) {
            Logger.getLogger(MultiThreadedTrapReceiver.class.getName())
  .log(Level.SEVERE, null, ex);
        }
        assert pduAgentAddress != null;
        System.out.println("hostname: " + pduAgentAddress.getHostAddress() 
    + "; port: " + nPort);
    }
    }

    public static void main(String[] args) {
    MultiThreadedTrapReceiver trap = new MultiThreadedTrapReceiver();
    }

    }

这是我的 SnmpUtilSendTrapV3 类,它将发送请求陷阱:

  package org.example.snmp;

  import java.io.IOException;
  import java.util.List;

 import org.snmp4j.*;
 import org.snmp4j.event.*;
 import org.snmp4j.mp.*;
 import org.snmp4j.security.*;
 import org.snmp4j.smi.*;
 import org.snmp4j.transport.*;

 public class SnmpUtilSendTrapV3 {

 private Snmp snmp = null;
 private Address targetAddress = null;
 private static final OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
 public void initComm() throws IOException {
 targetAddress = GenericAddress.parse("127.0.0.1/162");
 TransportMapping\<UdpAddress\> transport = new DefaultUdpTransportMapping();
 snmp = new Snmp(transport);
 snmp.listen();
  }


  public void sendPDU() throws IOException {
  UserTarget\<Address\> target = new UserTarget\<\>();
  target.setAddress(targetAddress);
  target.setRetries(2);
  target.setTimeout(1500);
  // snmp version
  target.setVersion(SnmpConstants.version3);


  target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
  target.setSecurityName(new OctetString("SHADES"));

  USM usm = new USM(SecurityProtocols.getInstance(),
        localEngineId, 0);
  usm.setEngineDiscoveryEnabled(true);
  SecurityModels.getInstance().addSecurityModel(usm);

  UsmUser user = new UsmUser(new OctetString("SHADES"),
        AuthSHA.ID,
        new OctetString("SHADESUserAuthPassword"),
        PrivAES256.ID,
        new OctetString("SHADESUserPrivPassword"));
  snmp.getUSM().addUser(new OctetString("SHADES"), user);

  // create PDU
  ScopedPDU pdu = new ScopedPDU();
  pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0"),
        new OctetString("SnmpTrapv3")));
  pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"),
        new OctetString("JavaEE")));
  pdu.setType(PDU.TRAP);

  // send PDU to Agent and receive Response
  ResponseEvent\<Address\> respEvnt = snmp.send(pdu, target);

  // analyze Response
  if (respEvnt != null && respEvnt.getResponse() != null) {
    List\<? extends VariableBinding\> recVBs =  respEvnt.getResponse().getVariableBindings();
    for (VariableBinding recVB : recVBs) {
        System.out.println(recVB.getOid() + " : " + recVB.getVariable());
    }
  }
  snmp.close();
  }

  public static void main(String\[\] args) {
     try {
       SnmpUtilSendTrapV3 util = new SnmpUtilSendTrapV3();
       util.initComm();
       util.sendPDU();
    } catch (IOException e) {
      e.printStackTrace();
   }
 }

 }

当我运行我的 SnmpUtilSendTrapV3 类时,我得到这个错误:

org.snmp4j.MessageException: Message processing model 3 returned error: 
Unsupported security level
at org.snmp4j.MessageDispatcherImpl.sendPdu(MessageDispatcherImpl.java:618)
at org.snmp4j.Snmp.sendMessage(Snmp.java:1054)
at org.snmp4j.Snmp.send(Snmp.java:943)
at org.snmp4j.Snmp.send(Snmp.java:937)
at org.snmp4j.Snmp.send(Snmp.java:905)
at org.example.snmp.SnmpUtilSendTrapV3.sendPDU(SnmpUtilSendTrapV3.java:60)
at org.example.snmp.SnmpUtilSendTrapV3.main(SnmpUtilSendTrapV3.java:76)

我的 MultiThreadedTrapReceiver 类没有任何东西只是等待 一些请求陷阱。

任何想法,我该怎么办?有什么我应该改变的吗?

但是当我使用 NOAUTH_NOPRIV 时,它工作得很好。

snmp snmp4j snmp-trap
© www.soinside.com 2019 - 2024. All rights reserved.