0

I have a requirement to handle the snmpset command from the cli to my application running on a specific port and send out a reply back to the snmpset command if it is success/failure.

Here are the steps I am doing

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

and I am running the listener in 11001. I am able to get the processPdu() in CommandResponder called successfully. After the Sync process is completed, I need to send a response back to the snmpset. How do I do that?

I tried snmp.send to the ip/port from where the command was received, but did not work.

Any inputs would really be helpful.

....

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);


    }
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
Vijay
  • 1
  • 2
  • Some code will help us understand your problem better – Karthik Prasad Sep 18 '14 at 18:18
  • Karthik, I have added the code snippet. Let me know if you have any suggestions. – Vijay Sep 19 '14 at 14:39
  • possible duplicate of [Getting started with SNMP4J](http://stackoverflow.com/questions/3224687/getting-started-with-snmp4j) – Jolta Sep 22 '14 at 08:04
  • target.setAddress(new UdpAddress(pearIP)); // the ip/port from where the snmpset command was issued. pdu.setType(PDU.RESPONSE); try { snmp.send(pdu, target); This is what I was looking for. I had missed out setting the PDU type to response earlier. Thanks guys for all the relevant links though. – Vijay Sep 22 '14 at 20:59
  • @Vijay would you be so kind to share your working code as an answer (and accept it if it worked for you)? I'm interested, thanks! :) – Matthieu Dec 28 '14 at 14:10

0 Answers0