- /* $Id: SnmpGet.java,v 1.1 2002/06/15 14:43:37 ram Exp $ */
- /*
- * @(#)SnmpGet.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 GET using com.adventnet.snmp.mibs package of
- * AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpGet hostname mibfile OID [OID]
- *
- * where
- *
- * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
- * mibfile is the name of the MIB file that is loaded. To load multiple mibs give within double quotes files seperated
- * by a blank space.
- * 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 .
- *
- * Example usage:
- *
- * java SnmpGet adventnet RFC1213-MIB 1.1.0 1.2.0 1.3.0 1.4.0
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.mibs.*;
- public class SnmpGet {
- public static void main(String args[]) {
- if( args.length < 3)
- {
- System.out.println("Usage : java SnmpGet hostname mibfile OID ");
- System.exit(0);
- }
- // Take care of getting hostname, OID and the MIB file name
- String remoteHost = args[0];
- String OID = args[1];
- String mibfile = args[2];
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- api.setDebug( true );
- // Open session
- SnmpSession session = new SnmpSession(api);
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // set remote Host
- session.setPeername(remoteHost);
- // load the MIB file
- MibOperations mibops = new MibOperations();
- try {
- mibops.loadMibModules(mibfile);
- } catch (Exception ex){
- System.err.println("Error loading MIBs: "+ex);
- }
- // Build Get request PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( api.GET_REQ_MSG );
- // add OIDs
- for (int i=2; i<args.length; i++){
- SnmpOID oid = mibops.getSnmpOID(args[i]);
- pdu.addNull(oid);
- }
- try {
- // Send PDU and receive response PDU
- pdu = session.syncSend(pdu);
- } catch (SnmpException e) {
- System.err.println("Error sending SNMP request: "+e);
- }
- // print the response pdu varbinds
- System.out.println(mibops.toString(pdu));
- // close session
- session.close();
- // stop api thread
- api.close();
- }
- }