- * @(#)SnmpSet.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is an example program to explain how to write an
- * application to set the value of sysName to "localhost" using
- * com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpSet
- *
- *
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpSet {
- public static void main(String args[]) {
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- // Open session
- SnmpSession session = new SnmpSession(api);
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- // Build set request PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand(SnmpAPI.SET_REQ_MSG );
- // Provide the OID .1.3.6.1.2.1.5.0
- SnmpOID oid = new SnmpOID("1.5.0");
- // provide the value "localhost"
- String value = "localhost";
- // provide the type "STRING"
- byte dataType = SnmpAPI.STRING;
- SnmpVar var = null;
- try
- {
- // create SnmpVar instance for the value and the type
- var = SnmpVar.createVariable(value, dataType);
- }
- catch(SnmpException e)
- {
- System.err.println("Cannot create variable " + oid + " with value " + value);
- return;
- }
- //create varbind
- SnmpVarBind varbind = new SnmpVarBind(oid, var);
- //add variable binding
- pdu.addVariableBinding(varbind);
- // Send PDU
- SnmpPDU result = session.syncSend(pdu);
- if (result == null)
- {
- System.out.println("Request timed out!");
- }
- else
- {
- if (result.getErrstat()== 0)
- {
- // print the response pdu varbinds
- System.out.println(result.printVarBinds());
- }
- else
- {
- System.out.println(result.getError());
- }
- }
- // close session
- session.close();
- // close api thread
- api.close();
- }
- }