[SNMP4J] Multiple Send Requests Doesn't Works.

RAVI GUPTA dce_ravigupta at yahoo.co.in
Wed Sep 10 12:12:10 CEST 2008


Dear All,

I had written a program using snmp4j to set values in a SNMP agent, but when I run the program and send SNMP Set requests, only the first set request sets the value in the agent and other not. Can anybody tell me what is the problem in my code.


import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SNMP 
{
    private Snmp snmp;
    private CommunityTarget target;
    private PDU requestPDU;
    
    /**
     * Create a new SNMP session.
     * 
     * @throws java.io.IOException - if session cannot be created successfully.
     */
    public SNMP() throws IOException
    {
        snmp = new Snmp(new DefaultUdpTransportMapping());
        snmp.listen();
        
        target = new CommunityTarget();
        target.setVersion(SnmpConstants.version1);
        target.setRetries(3);
        
        requestPDU = new PDU();
        requestPDU.setNonRepeaters(0);
        requestPDU.add(new VariableBinding());
    }

    /**
     * Sends a SNMP set request to the specified target.
     * @param address IP address of the target.
     * @param objectID Object ID of the target.
     * @param value Value to set.
     * @throws java.io.IOException if the message could not be send.
     */
    public void send(String address, String objectID, int value) throws IOException
    {
        target.setCommunity(new OctetString("public"));
        target.setAddress(GenericAddress.parse("udp:" + address + "/161"));
        
        requestPDU.setType(PDU.SET);
        requestPDU.set(0, new VariableBinding(new OID(objectID), new Integer32(value)));
        snmp.send(requestPDU, target);
    }
    
    public void close() throws IOException
    {
        snmp.close();
    }
    
    public static void main(String args[]) throws Exception
    {
        SNMP snmp = new SNMP();
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.0" , 2);
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.1" , 3);
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.2" , 4);
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.3" , 5);
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.4" , 6);
        snmp.send("10.20.50.232", ".1.3.6.1.4.1.33000.10.1.5" , 7);
        snmp.close();
    }
    
}

Thanks in Advance.




      Add more friends to your messenger and enjoy! Go to http://in.messenger.yahoo.com/invite/


More information about the SNMP4J mailing list