1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| package in.techdive.snmp4j.ex;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SnmpSetExample
{
private static String ipAddress = "127.0.0.1";
private static String port = "8001";
// sysContact OID of MIB RFC 1213; Scalar Object = .iso.org.dod.internet.mgmt.mib-2.system.sysContact.0
private static String sysContactOid = ".1.3.6.1.2.1.1.4.0"; // ends with 0 for scalar object
private static String sysContactValue = "TechDive.in";
private static int snmpVersion = SnmpConstants.version1;
private static String community = "public";
public static void main(String[] args) throws Exception
{
System.out.println("SNMP SET Demo");
// Create TransportMapping and Listen
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
// Create Target Address object
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString(community));
comtarget.setVersion(snmpVersion);
comtarget.setAddress(new UdpAddress(ipAddress + "/" + port));
comtarget.setRetries(2);
comtarget.setTimeout(1000);
// Create the PDU object
PDU pdu = new PDU();
// Setting the Oid and Value for sysContact variable
OID oid = new OID(sysContactOid);
Variable var = new OctetString(sysContactValue);
VariableBinding varBind = new VariableBinding(oid,var);
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
// Create Snmp object for sending data to Agent
Snmp snmp = new Snmp(transport);
System.out.println("\nRequest:\n[ Note: Set Request is sent for sysContact oid in RFC 1213 MIB.");
System.out.println("Set operation will change the sysContact value to " + sysContactValue );
System.out.println("Once this operation is completed, Querying for sysContact will get the value = " + sysContactValue + " ]");
System.out.println("Request:\nSending Snmp Set Request to Agent...");
ResponseEvent response = snmp.set(pdu, comtarget);
// Process Agent Response
if (response != null)
{
System.out.println("\nResponse:\nGot Snmp Set Response from Agent");
PDU responsePDU = response.getResponse();
if (responsePDU != null)
{
int errorStatus = responsePDU.getErrorStatus();
int errorIndex = responsePDU.getErrorIndex();
String errorStatusText = responsePDU.getErrorStatusText();
if (errorStatus == PDU.noError)
{
System.out.println("Snmp Set Response = " + responsePDU.getVariableBindings());
}
else
{
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);
}
}
else
{
System.out.println("Error: Response PDU is null");
}
}
else
{
System.out.println("Error: Agent Timeout... ");
}
snmp.close();
}
} |
Partager