- * @(#)AddRow.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
- * add a arow to an SMIv1 table if the column oids are .1.3.6.1.4.1.2.2.1.1,
- * .1.3.6.1.4.1.2.2.1.2, .1.3.6.1.4.1.2.2.1.3 and .1.3.6.1.4.1.2.2.1.3
- * by using com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
- *
- * The user could run this application by giving the following usage.
- *
- * java AddRow
- *
- *
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class AddRow {
- public static void main(String args[]) {
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- // Open session
- SnmpSession session = new SnmpSession(api);
- session.open();
- // Build Get request PDU
- SnmpPDU pdu = new SnmpPDU();
- UDPProtocolOptions option = new UDPProtocolOptions("localhost");
- pdu.setProtocolOptions(option);
- pdu.setCommand(SnmpAPI.SET_REQ_MSG);
- //get oids
- SnmpOID oid[]=new SnmpOID[4];
- oid[1]=new SnmpOID(".1.3.6.1.4.1.2.2.1.1.15");
- oid[2]=new SnmpOID(".1.3.6.1.4.1.2.2.1.2.15");
- oid[3]=new SnmpOID(".1.3.6.1.4.1.2.2.1.3.15");
- oid[4]=new SnmpOID(".1.3.6.1.4.1.2.2.1.4.15");
- //create SnmpVar objects
- SnmpVar var[]=new SnmpVar[4];
- try
- {
- // create SnmpVar instances
- var[0]=SnmpVar.createVariable("15",SnmpAPI.INTEGER);
- var[1]=SnmpVar.createVariable("switch2",SnmpAPI.STRING);
- var[2]=SnmpVar.createVariable("9000",SnmpAPI.INTEGER);
- var[3]=SnmpVar.createVariable("2",SnmpAPI.INTEGER); // the value 2 refers to createRequest.
- }
- catch(SnmpException e)
- {
- System.err.println("Cannot create one of the variables");
- return;
- }
- // add varbinds
- for(int i=0;i<oid.length;i++)
- {
- SnmpVarBind varbind=new SnmpVarBind(oid[i],var[i]);
- pdu.addVariableBinding(varbind);
- }
- // Send PDU and receive response PDU
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- 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();
- }
- }