Announcement

Collapse
No announcement yet.

SNMP4J set don't works

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SNMP4J set don't works

    Hi,
    I'm excuting this snmp set by snmp4J:

    snmpset -c private -v 2c 192.168.1.26 .1.3.6.1.4.1.991.100.5.19.3.0 u 1

    This is the code:

    Code:
    public class SnmpSetExample {
    
    private static String ipAddress = "192.168.1.26";
    private static String port = "161";
    private static String sysContactOid = ".1.3.6.1.4.1.991.100.5.19.3.0";
    private static int sysContactValue = 1;
    private static int snmpVersion = SnmpConstants.version1;
    private static String community = "private";
    
    public static void main(String[] args) throws Exception {
        System.out.println("SNMP SET Demo");
    
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
    
        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString(community));
        comtarget.setVersion(snmpVersion);
        comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
        comtarget.setRetries(2);
        comtarget.setTimeout(1000);
    
        PDU pdu = new PDU();
    
        // Setting the Oid and Value for sysContact variable
        OID oid = new OID(sysContactOid);
    
        Variable var = new Integer32(sysContactValue);
        VariableBinding varBind = new VariableBinding(oid, var);
        pdu.add(varBind);
    
        pdu.setType(PDU.SET);
        pdu.setRequestID(new Integer32(1));
    
        // Create Snmp object for sending data to Agent
        Snmp snmp = new Snmp(transport);
    
        ResponseEvent response = snmp.set(pdu, comtarget);
    
        if (response != null) {
            System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
            PDU responsePDU = response.getResponse();
    
            if (responsePDU != null) {
                int errorStatus = responsePDU.getErrorStatus();
                int errorIndex = responsePDU.getErrorIndex();
                String errorStatusText = responsePDU.getErrorStatusText();
    
                if (errorStatus == PDU.noError) {
                    System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
                } else {
                    System.out.println("Error: Request Failed");
                    System.out.println("Error Status = " + errorStatus);
                    System.out.println("Error Index = " + errorIndex);
                    System.out.println("Error Status Text = " + errorStatusText);
                }
            } else {
                System.out.println("Error: Response PDU is null");
            }
        } else {
            System.out.println("Error: Agent Timeout... ");
        }
        snmp.close();
    }
    But my error result is:

    Code:
    ...
    Response: 
    Got Snmp Set Response from Agent 
    Error: Response PDU is null
    PDU is ready setted to .SET. Somebody know what's missing in the parameters or the code?. Thanks in advance.

  • #2
    Solution:

    in: Variable var = new Integer32(sysContactValue);

    Change Gauge32 or UnsignedInterger32 insted of Integer32 type. It is necessary to always check the data type of the value to send.

    Comment

    Working...
    X