- /*
- * @(#)SnmpGet_tcp.java
- * Copyright (c) 2000 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.snmp2 package of
- * AdventNetSNMP api. This example explains how to plug-in the independent transport
- * provider framework in the applications. It uses tcp for communication instead of the
- * default UDP.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpGet_tcp hostname OID [OID]
- *
- * 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 .
- *
- * Please note that the remote agent should be able to access the tcp packets.
- *
- * Example usage:
- *
- * java SnmpGet_tcp adventnet 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.*;
- public class SnmpGet {
- public static void main(String args[]) {
- if( args.length < 2)
- {
- System.out.println("Usage : java SnmpGet hostname OID ");
- System.exit(0);
- }
- // Take care of getting the hostname and the OID
- String remoteHost = args[0];
- String OID = args[1];
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- api.setDebug( true );
- // Open session
- SnmpSession session = new SnmpSession(api);
- //choose your transport provider
- session.setProtocol(session.TRANSPORT_PROVIDER);
- // set the protocol to the TCP
- ProtocolOptions options = new TcpProtocolOptionsImpl(remoteHost, 161, 0);
- session.setProtocolOptions(options);
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // set remote Host
- //session.setPeername(remoteHost);
- // Build Get request PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( api.GET_REQ_MSG );
- // add OIDs
- for (int i=1; i<args.length; i++){
- SnmpOID oid = new SnmpOID(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(pdu.printVarBinds());
- // close session
- session.close();
- // stop api thread
- api.close();
- }
- }