0
votes

i'm trying to create simple example of snmp manager and agent. all i need to do is just read the current value from given OID, write some new value to replace it, and read it back.

my get function working well but my set doesn't. in the line: PDU responsePDU = event.getResponse(); responsePDU equal to null and i don't understand why? any help will be very welcome!

    public ResponseEvent set(OID oid,String val) throws IOException 
{
    PDU pdu = new PDU();
    VariableBinding varBind = new VariableBinding(oid,new OctetString(val));
    pdu.add(varBind);
    pdu.setType(PDU.SET);
    pdu.setRequestID(new Integer32(1));
    Target target=getTargetForWrite();

    ResponseEvent event = snmp.set(pdu, target);
    if(event != null)
    {
      System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
      System.out.println("Snmp Set Request = " + event.getRequest().getVariableBindings());
      PDU responsePDU = event.getResponse();
      System.out.println("\nresponsePDU = "+responsePDU);
      if (responsePDU != null)
      {
        int errorStatus = responsePDU.getErrorStatus();
        int errorIndex = responsePDU.getErrorIndex();
        String errorStatusText = responsePDU.getErrorStatusText();
        System.out.println("\nresponsePDU = "+responsePDU);
        if (errorStatus == PDU.noError)
        {
          System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
        }
        else
        {
          System.out.println("errorStatus = "+responsePDU);
          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);
        }
       }

      return event;
    }
    throw new RuntimeException("GET timed out");
}



    private Target getTargetForWrite()
{
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("private"));
    target.setVersion(SnmpConstants.version2c);
    target.setAddress(GenericAddress.parse(address));
    target.setRetries(2);
    target.setTimeout(1500);

    return target;
}
1
Don't you need a response resulting from a snmp.send instead of a snmp.set to execute the getResponse from? Just guessing, not working with snmp4j in practice. Maybe also provide the import satements of external "things" referenced would help the knowing ones to more easily help you.Dilettant
i'm just starting to learn snmp but as i understand snmp.send used for get command and snmp.set for set commandyogev levi

1 Answers

0
votes

need to set the community as public

private Target getTargetForWrite()
{
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));\\<--- 
    target.setVersion(SnmpConstants.version2c);
    target.setAddress(GenericAddress.parse(address));
    target.setRetries(2);
    target.setTimeout(1500);

    return target;
}