- /* $Id: snmpset.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
- /*
- * @(#)snmpset.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a tutorial example program to explain how to write an application to do
- * the basic SNMP operation SET using com.adventnet.snmp.snmp2 package of
- * AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java snmpset hostname OID type value
- *
- * where
- *
- * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
- *
- * OID is the Object Identifier. Multiple OIDs can also be given.
- * The entire OID can be given or it can be given in the form of 1.1.0.
- * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
- * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 .
- *
- * type is the Object type (STRING, INTEGER, COUNTER etc)
- *
- * value is the object instance value to be set
- *
- * Example usage:
- *
- * java snmpset adventnet 1.6.0 STRING testing
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpSet {
- public static void main(String args[]) {
- if( args.length < 4)
- {
- System.out.println("Usage: java SnmpSet hostname OID type value");
- System.exit(0);
- }
- // get the hostname, OID, type and value
- String remoteHost = args[0];
- String OID = args[1];
- String type = args[2];
- String value = args [3];
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- // Open session
- SnmpSession session = new SnmpSession(api);
- // set remote Host
- session.setPeername(remoteHost);
- //open session
- 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 );
- // add OID
- SnmpOID oid = new SnmpOID(OID);
- // get the type
- byte dataType;
- if (type.equals("STRING"))
- {
- dataType = SnmpAPI.STRING;
- }
- else
- {
- System.err.println("Invalid variable type: " + type);
- return;
- }
- // create SnmpVar instance for the value and the type
- SnmpVar var = null;
- try
- {
- 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
- try
- {
- SnmpPDU result = session.syncSend(pdu);
- }
- catch (SnmpException e)
- {
- System.err.println("Sending PDU"+e.getMessage());
- }
- // print the response pdu varbinds
- System.out.println("Response PDU received from " +result.getAddress()+ ", community: " + result.getCommunity());
- System.out.println(result.printVarBinds());
- // close session
- session.close();
- // close the api thread
- api.close();
- System.exit(0);
- }
- }