public class MYSNMP {
public String SNMP_GET(String ipAddress, int port, String strOID, String community) {
String strResponse = "";
ResponseEvent response;
Snmp snmp;
try {
OctetString community1 = new OctetString(community);
String host = ipAddress + "/" + port;
Address tHost = new UdpAddress(host);
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(community1);
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(tHost);
comtarget.setRetries(2);
comtarget.setTimeout(5000);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(strOID)));
pdu.setType(PDU.GET);
snmp = new Snmp(transport);
response = snmp.get(pdu, comtarget);
if (response != null) {
if (response.getResponse().getErrorStatusText().equalsIgnoreCase("Success")) {
PDU pduresponse = response.getResponse();
strResponse = pduresponse.getVariableBindings().firstElement().toString();
if (strResponse.contains("=")) {
String strNewResponse[] = null;
int len = strResponse.indexOf("=");
strNewResponse = strResponse.split("=");
//System.out.println("The SNMP response to the OID requested is : " + strNewResponse[1]); //FOR DEBUG
strResponse = strNewResponse[1].trim();
}
}
} else {
Freedomotic.logger.severe("TimeOut occured");
}
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
return strResponse;
}