corbatable.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:10k
- /* $Id: corbatable.src,v 1.3 2002/09/09 05:45:01 tonyjpaul Exp $ */
- /*
- * @(#)corbatable.java
- * Copyright (c) 1996-2002 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 do
- * the table operation using com.adventnet.snmp.corba of AdventNetSNMP2 api.
- * The user could run this application by giving any one of the following usage.
- *
- * java corbatable [ options ] host tableOID
- *
- *<img SRC="images/v3only.jpg" ALT="v3 only"> v3 request:
- * java corbatable [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] [-n context_name] host tableOID ...
- * e.g.
- * java corbatable -v v3 -a MD5 -w passwd -u username -p 2000 -c public -m ....mibsrfc1213-mib localhost tcpConnTable
- *
- *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v2c request:
- * java corbatable [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host tableOID ...
- * e.g.
- * java corbatable -v v2 -c public -m ....mibsrfc1213-mib localhost tcpConnTable
- *
- * v1 request:
- * java corbaget [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
- * e.g.
- * java corbatable -m ....mibsrfc1213-mib localhost tcpConnTable
- *
- *
- * Options:
- * [-SnmpServer ] <hostName> - SnmpServer SERVER option to specify the
- CORBA server host.
- * [-ORBInitialPort ] <orbPort> - port on which transient name server is running
- * [-ORBInitialHost ] <orbHost> - host name of transient name server
- * [-c] <community> - community String. By default "public".
- * [-p] <port> - remote port no. By default 161.
- * [-t] <Timeout> - Timeout. By default 5000ms.
- * [-r] <Retries> - Retries. By default 0.
- * [-m] <MIBfile> - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.
- *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version> - version(v1 / v2 / v3). By default v1.
- * [-u] <username> - The v3 principal/userName
- * [-a] <autProtocol> - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
- * [-w] <authPassword> - The authentication password.
- * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
- * [-i] <contextID> - The contextID to be used for the v3 pdu.
- * [-n] <contextName> - The contextName to be used for the v3 pdu.
- * host Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
- * tableOID Mandatory - Give multiple no. of table Object Identifiers.
- */
- import org.omg.CosNaming.*;
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import org.omg.CORBA.*;
- import com.adventnet.snmp.corba.*;
- import ParseOptions;
- import com.adventnet.snmp.mibs.*;
- import com.adventnet.snmp.snmp2.*;
- public class corbatable {
- static SnmpTable table;
- public static void main(String args[]) {
- String usage = "corbatable [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] [-n context_name] host tableOID ...";
- String options[] = { "-SnmpServer", "-ORBInitialPort", "-ORBInitialHost", "-c", "-p", "-r", "-t", "-m","-v", "-u", "-a", "-w", "-s", "-i", "-n"};
- String values[] = { null,null,null, null, null, null, null, null, null, null,null,null,null,null,null};
-
- String userName = new String("");
- String authProtocol = new String("NO_AUTH");
- String authPassword = new String ("");
- String privPassword = new String ("");
- String contextID = new String ("");
- String contextName = new String ("");
- final int USM_SECURITY_MODEL = 3;
-
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- // check for at least hostname and one OID in remaining arguments
- if (opt.remArgs.length<2) opt.usage_error();
- try
- {
- // Initialise the ORB
- System.out.println( "Initialising the ORB" );
- org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( args, null );
- // Use Naming Service
- org.omg.CORBA.Object objRef = orb.resolve_initial_references(
- "NameService" );
- System.out.println( "Resolved NameService" );
- NamingContext ncRef = NamingContextHelper.narrow( objRef );
- // bind the Object Reference in Naming
- NameComponent nc = new NameComponent( "AdventnetSnmpFactory" , "" );
- NameComponent[] path = { nc };
-
- // Bind to the SnmpTarget object
- //System.out.println( "Binding to the Factory object" );
- //CORBAsnmp.SnmpTargetFactory factory = CORBAsnmp.SnmpTargetFactoryHelper.
- //bind( orb, "AdventnetSnmpTargetFactory" );
- System.out.println( "Binding to the Factory object" );
- com.adventnet.snmp.corba.SnmpFactory factory = com.adventnet.snmp.corba.SnmpFactoryHelper.narrow( ncRef.resolve( path ) );
- System.out.println( "Bound to the Factory object" );
- // Get a Target object
- table = factory.createTable();
- System.out.println( "Got the SnmpTarget object" );
- //call SNMP methods from client
- //call SNMP methods from client
- if(values[8] != null)
- { // if SNMP version is specified, set it
- if(values[8].equals("v1"))
- table.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION1 ) ;
- else if(values[8].equals("v2"))
- table.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION2C );
- else if(values[8].equals("v3"))
- table.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION3 );
- else
- {
- System.out.println("Invalid Version Number");
- System.exit(1);
- }
- }
- else
- table.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION1 );
-
- table.setTargetHost( opt.remArgs[0] ); // set the agent hostname
- if (values[3] != null) // set the community if specified
- table.setCommunity( values[3] );
- // set the timeout/retries/port parameters, if specified
- if (values[4] != null)
- table.setTargetPort( Integer.parseInt(values[4]) );
- if (values[5] != null)
- table.setRetries( Integer.parseInt(values[5]) );
- if (values[6] != null)
- table.setTimeout( Integer.parseInt(values[6]) );
- if (values[9] != null)
- userName = values[9];
- if (values[10] != null)
- authProtocol = values[10];
- if (values[11] != null)
- authPassword = values[11];
- if (values[12] != null)
- privPassword = values[12];
- if (values[13] != null)
- contextID = values[13];
- if (values[14] != null)
- contextName = values[14];
-
-
- if(table.getSnmpVersion() == com.adventnet.snmp.beans.SnmpTarget.VERSION3)
- {
- table.setPrincipal(userName);
- table.setAuthPassword(authPassword);
- table.setPrivPassword(privPassword);
- table.setContextID(contextID);
- table.setContextName(contextName);
- if(authProtocol.equals("SHA"))
- {
- table.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.SHA_AUTH);
- }
- else
- {
- table.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.MD5_AUTH);
- }
- table.create_v3_tables();
- }
- if (values[7] != null)
- // Load the MIB files
- {
- System.err.println("Loading MIBs: "+values[7]);
- table.loadMibs(values[7]);
- }
- // instantiate this class to receive table events, and register it
- com.adventnet.snmp.corba.SnmpTableListener listener = new SnmpTableListenerImpl( table );
- orb.connect( listener );
- table.addSnmpTableListener(listener);
- // Set the table OID in SnmpTable. Once specified,
- // SnmpTable will automatically get the data and send table events
- try {
- table.setTableOID( opt.remArgs[1] );
- } catch (Exception ex) {
- System.err.println("Invalid table OID: "+ex);
- System.exit(1);
- }
- System.out.println("Getting table. Table items:");
- /*
- java.lang.Object sync = new java.lang.Object();
- //System.out.println( "Waiting for traps ... " );
- synchronized ( sync )
- {
- sync.wait();
- }
- */
- try { Thread.sleep(300000); } // allow some time to get all rows
- catch (InterruptedException ex) {}
- // All done; destroy the server-side Target object
- factory.destroyTable( table.getName() );
- } catch (Exception ex) {
- System.err.println("Failed: "+ ex);
- ex.printStackTrace();
- }
- System.exit(0);
- }
- }