I am trying to write my first snmp4j client. I have an agent running on 192.168.60.105. Using net-snmp I can query an OID and get a result. Using smnp4j the response event for the snmp get is returned with a null response and a null error. I think the message is timing out but I don't know why.
I get a result using net-snmp
jgaer@ljgaer2_~: snmpget 192.168.60.105 .1.3.6.1.4.1.27675.20.5.2.0
CW-NET-STG-SVR-MIB::cwNetStgSvrProvisioningEnable.0 = Hex-STRING: 00 00 00 00
I've tried using a longer timeout and a larger number of retries and it takes longer to return. That's why I think I am timing out. I just don't understand why. I also would have expected that if a responseEvent is returned on a timeout the error would indicate that. I've tried using verions1. Using version 3 required a scoped PDU.
public static void main(String[] args) throws IOException {
String address = ("udp:192.168.60.105/161");
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
PDU pdu = new PDU();
pdu.setType(PDU.GET);
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.27675.20.5.2.0")));
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
ResponseEvent response = snmp.send(pdu,target);
System.out.println(response);
System.out.println(response.getResponse());
System.out.println(response.getError());
}
results from running the above code
org.snmp4j.event.ResponseEvent[source=org.snmp4j.Snmp@3f91beef]
null
null
I would expect that either the error or the response would be non null. I am using java version java version "1.8.0_191" and snmp4j version 2.5.0. The agent is running 2.5.3
I traced the packets using wireshark and can confirm that I am never getting a response from the agent using snmp4j.I don't really understand enough of the protocal to do a byte by byte comparison, but the info column for the net-snmp call looks very different from snmp4j calls.
net-snmp
length info
106 get-request : sent from client to agent
159 report 1.3.6.1.6.3.15.1.1.4.0 : sent from agent to client
192 encryptedPDI : privKey unknown : sent from client to agent
196 encryptedPDI : privKey unknown : sent from agent to client
snmp4j - response never received three messages from client to agent
89 get-request 1.3.6.1.4.1.27675.20.5.2.0 sent from client to agent
Looking at the text encoded version of the bytes I see the string 'public'