无法通过adventnet

时间:2015-06-26 06:43:08

标签: java snmp

我跟着this page创建了一个TrapListener,我的代码如下所示:

public class SnmpTrapd implements TrapListener {  
    public static void main(String args[]) {  
        // instantiate SNMP Trap Receiver bean  
        SnmpTrapReceiver trapreceiver = new SnmpTrapReceiver();  
        // set the port in which the trap is received  
        trapreceiver.setPort(162);  
        // register the listener for trap events  
        trapreceiver.setAutoInformResponse(true);   
        trapreceiver.setLocalAddresses(new String[]{new String("192.168.1.2")});
        trapreceiver.addTrapListener(new SnmpTrapd());  
        trapreceiver.setTrapAuthEnable(false);
        System.out.println("Waiting to receive traps .......");  

    }  

    @Override  
    public void receivedTrap(TrapEvent trap) {  
        System.out.println("Got a trap from: " + trap.getRemoteHost());  
        // print PDU details  
        System.out.println(((SnmpTrapReceiver) trap.getSource()).getMibOperations().toString(trap.getTrapPDU()));  
        if (trap.getTrapPDU().getCommand() == SnmpAPI.TRP_REQ_MSG) {  
            com.adventnet.snmp.mibs.MibTrap trapDefn = trap.getTrapDefinition();  
            if (trapDefn != null) // print name and description  
                System.out.println("Trap Name: " + trapDefn.getName() + "\nDescr: " + trapDefn.getDescription());  
        }  
    }  
}  

然而,当我使用Fortigate 60D创建snmp v3陷阱时,它没有收到任何信息。我确定陷阱是从fortigate发出的,因为我用wireshark监视了计算机上的界面。

更重要的是,当我使用另一个api(而不是adventnet)时,我可以收到v3陷阱,所以我很确定fortigate的设置是正确的。
我的代码有什么问题吗?

更新

我也尝试了this page所说的,但仍然徒劳无功。
(虽然我想知道该页面是关于v2c陷阱而不是v3 ......)

2 个答案:

答案 0 :(得分:0)

You set setTrapAuthEnable to false which means you would like to drop some v3 TRAP messages. Is that what you expected? Read the documentation and also check the packets sent by 60D, then you will see if that's the cause.

答案 1 :(得分:0)

陷阱接收器工作示例。

public class TestTrapV3_2 implements CommandResponder {

    private static final String _V3_USERNAME = "newUser";
    private static final String _V3_AUTHENTICATION_PASSPHRASE = "abc12345";

    private static final String _V3_PRIVACY_PASSPHRASE = "abc12345";

    public static void main(String[] args) throws IOException {

        TestTrapV3_2 trap = new TestTrapV3_2();
        trap.startTrapReceiver();
    }

    private synchronized void startTrapReceiver() throws IOException {
        ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);

        Address listenAddress = new UdpAddress("localhost/162");
        TransportMapping transport = null;
        if (listenAddress instanceof UdpAddress) {
            transport = new DefaultUdpTransportMapping(
                    (UdpAddress) listenAddress);
        } else {
           /* transport = new DefaultTcpTransportMapping(
                    (TcpAddress) listenAddress);*/
        }
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                MPv3.createLocalEngineID()), 0);
        usm.setEngineDiscoveryEnabled(true);

        MessageDispatcher mDispathcher = new MultiThreadedMessageDispatcher(
                threadPool, new MessageDispatcherImpl());

        // add message processing models
        mDispathcher.addMessageProcessingModel(new MPv1());
        mDispathcher.addMessageProcessingModel(new MPv2c());
        mDispathcher.addMessageProcessingModel(new MPv3(usm));
        // add all security protocols
        SecurityProtocols.getInstance().addDefaultProtocols();
        SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES128());

        SecurityModels.getInstance().addSecurityModel(usm);

        CommunityTarget target = new CommunityTarget();

        target.setCommunity(new OctetString("public"));

        Snmp snmp = new Snmp(mDispathcher, 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);
        snmp.addCommandResponder(this);

        transport.listen();
        System.out.println("listening");
        try {
            this.wait();
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    @Override
    public void processPdu(CommandResponderEvent arg0) {
        System.out.println("Received PDU...");
        PDU pdu = arg0.getPDU();

        if (pdu != null) {
            System.out.println("Security level = "+ arg0.getSecurityLevel() );
            System.out.println("Peer Address = "+ arg0.getPeerAddress() );
            System.out.println("Trap Type = " + pdu.getType());
            System.out.println("Variables = " + pdu.getVariableBindings());
            System.out.println("**************************");
        }
    }
}