Thứ Ba, 12 tháng 3, 2013

How to perform SNMP SET/GET using SNMP4J

SNMP4J – Free Open Source SNMP API for Java, enables java developers to write code to perform SNMP operations by using it’s api. There are many other api’s available to do this job, but SNMP4J is free to download and use, whereas other api most often comes with license that need to be purchased. SNMP4J can be downloaded from here.

SNMP Get and SNMP Set are two most commonly used functions among other SNMP operations available. Below is the sample code to use SNMP4J api to perform these two operations using Java.

Hope this information helped in writing your code a little quickly…!! Please leave your suggestions and feedback in the comments section below.

SNMP SET Using SNMP4J




import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class SnmpTest {

public void snmpSet(String host, String community, String strOID, int Value) {
host= host+"/"+"161";
Address tHost = GenericAddress.parse(host);
Snmp snmp;
try {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(tHost);
target.setRetries(2);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version1); //Set the correct SNMP version here
PDU pdu = new PDU();
//Depending on the MIB attribute type, appropriate casting can be done here
pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value)));
pdu.setType(PDU.SET);
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
PDU strResponse;
String result;
((Snmp)event.getSource()).cancel(event.getRequest(), this);
strResponse = event.getResponse();
if (strResponse!= null) {
result = strResponse.getErrorStatusText();
System.out.println("Set Status is: "+result);
}
}};
snmp.send(pdu, target, null, listener);
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}



 

SNMP GET Using SNMP4J




 
public String snmpGet(String host, String community, String strOID) {
String strResponse="";
ResponseEvent response;
Snmp snmp;
try {
OctetString community1 = new OctetString(community);
host= host+"/"+"161";
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("=")) {
int len = str.indexOf("=");
strResponse=str.substring(len+1, str.length());
}
}
} else {
System.out.println("Looks like a TimeOut occured ");
}
snmp.close();
} catch(Exception e) {
e.printStackTrace();
}
//System.out.println("Response="+strResponse);
return strResponse;
}


0 nhận xét:

Đăng nhận xét