public static void snmpSet(String address, String community,
String oid, int value, int port) throws SNMPException {
address = address + "/" + port;
Address targetAddress = GenericAddress.parse(address);
Snmp snmp;
try {
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(0);
target.setTimeout(200);
target.setVersion(SnmpConstants.version2c);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid), new Integer32(value)));
pdu.setType(PDU.SET);
ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
// Always cancel async request when response has been received
// otherwise a memory leak is created! Not canceling a request
// immediately can be useful when sending a request to a broadcast
// address.
((Snmp)event.getSource()).cancel(event.getRequest(), this);
}
};
snmp.send(pdu, target, null, listener);
snmp.close();
}
catch (IOException e) {
String message = "No set snmp. IO exception: " + e.getMessage();
logger.error(message);
throw new SNMPException(message);