需要从snmp4j响应来自CLI的snmpset

时间:2014-09-18 18:01:26

标签: snmp net-snmp snmp4j

我需要处理从cli到我在特定端口上运行的应用程序的snmpset命令,并在成功/失败时发送回snmpset命令。

以下是我正在做的步骤

  

snmpset -v 2c -r 0 -c public 10.193.154.68:11001 test s resync

我在11001运行监听器。我能够在CommandResponder中成功调用processPdu()。 同步过程完成后,我需要将响应发送回snmpset。我怎么做?

我尝试将snmp.send发送到收到命令的ip / port,但是没有用。

任何输入都会有所帮助。

...

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", 30);
        MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());

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

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

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

        snmp = new Snmp(mtDispatcher, transport);
        snmp.addCommandResponder(this);

        log.info("TrapListener listening on "+address);
        transport.listen();
        try
        {
            this.wait();
        }
        catch (InterruptedException ex)
        {
            Thread.currentThread().interrupt();
        }
    }

    public synchronized void processPdu(CommandResponderEvent cmdRespEvent)
    {
        PDU pdu = cmdRespEvent.getPDU();
        ..
        ..

        // here I want to send a reply back to the snmpset command issued from CLI. 
        //snmpset -v 2c -r 0 -c public 10.193.154.68:11001 test s resync 


        // I tried sending it back to the ip/port from which the message was received
        ..
        ..
        snmp = new Snmp(mtDispatcher, transport);
        snmp.addCommandResponder(this);


        ResponseEvent response = snmp.send(pdu, target);


    }

0 个答案:

没有答案