corbagetbulk.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:10k
- /*
- * @(#)corbagetbulk.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 basic SNMP operation GETBULK using com.adventnet.snmp.corba of AdventNetSNMP2 api.
- * The user could run this application by giving any one of the following usage.
- *
- * java corbagetbulk [options] hostname oid ...
- *
- * v3 request:
- * java corbagetbulk [-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 contextName] host OID [OID] ...
- * e.g.
- * java corbagetbulk -v v3 -a MD5 -w passwd -u username -p 2000 -c public -m ....mibsrfc1213-mib adventnet 1.1.0 1.2.0
- *
- * v2c request:
- * java corbagetbulk [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
- * e.g.
- * java corbagetbulk -p 161 -v v2 -c public -m ....mibsrfc1213-mib adventnet 1.1.0 1.2.0
- *
- *
- * 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.
- * <nonRepeaters> - non-repeaters. Mandatory
- * <maxRepetions> - max-repetitions. Mandatory
- * [-m] <MIBfile> - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.
- * [-v] <version> - version(v1 / v2 / v3). By default v2c.
- * [-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 contextIDto 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).
- * OID Mandatory - Give multiple no. of Object Identifiers.
- */
- import org.omg.CosNaming.*;
- import java.io.*;
- import org.omg.CORBA.*;
- import com.adventnet.snmp.corba.*;
- import ParseOptions;
- public class corbagetbulk
- {
- public static void main( String[] args )
- {
- // Take care of getting options
- String usage = "corbagetbulk [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-v version(v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-nr non-repeaters] [-mr max-repetitions] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] [-n contextName] host OID [OID] ...";
- String options[] = { "-SnmpServer", "-ORBInitialPort", "-ORBInitialHost", "-c", "-p", "-t", "-r", "-m", "-nr", "-mr","-v","-u", "-a", "-w", "-s","-i","-n" };
- String values[] = { null, null, null, 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" );
- 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" );
- SnmpFactory factory = com.adventnet.snmp.corba.SnmpFactoryHelper.narrow( ncRef.resolve( path ) );
- System.out.println( "Bound to the Factory object" );
- // Get a Target object
- com.adventnet.snmp.corba.SnmpTarget target = factory.createTarget();
- System.out.println( "Got the SnmpTarget object" );
- if(values[10] != null)
- { // if SNMP version is specified, set it
- if(values[10].equals("v2"))
- target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION2C ) ;
- else if(values[10].equals("v3"))
- target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION3 );
- else
- {
- System.out.println("Invalid Version Number");
- System.exit(1);
- }
- }
- else
- target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION2C ) ;
-
- int nonRepeaters = 0;
- target.setTargetHost( opt.remArgs[0] ); // set the agent hostname
- System.out.println( "setTargetHost() called " );
- if (values[3] != null) // set the community if specified
- target.setCommunity( values[3] );
- try
- { // set the timeout/retries/port parameters, if specified
- if (values[4] != null)
- target.setTargetPort( Integer.parseInt(values[4]) );
- if (values[5] != null)
- target.setRetries( Integer.parseInt(values[5]) );
- if (values[6] != null)
- target.setTimeout( Integer.parseInt(values[6]) );
- if (values[8] != null)
- nonRepeaters = Integer.parseInt( values[8] );
- target.setNonRepeaters( nonRepeaters );
- if (values[9] != null)
- target.setMaxRepetitions( Integer.parseInt(values[9]) );
- if (values[11] != null)
- userName = values[11];
- if (values[12] != null)
- authProtocol = values[12];
- if (values[13] != null)
- authPassword = values[13];
- if (values[14] != null)
- privPassword = values[14];
- if (values[15] != null)
- contextID = values[15];
- if (values[16] != null)
- contextName = values[16];
-
-
- if(target.getSnmpVersion() == com.adventnet.snmp.beans.SnmpTarget.VERSION3)
- {
- target.setPrincipal(userName);
- target.setAuthPassword(authPassword);
- target.setPrivPassword(privPassword);
- target.setContextID(contextID);
- target.setContextName(contextName);
- if(authProtocol.equals("SHA"))
- {
- target.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.SHA_AUTH);
- }
- else
- {
- target.setAuthProtocol(com.adventnet.snmp.beans.SnmpTarget.MD5_AUTH);
- }
- target.create_v3_tables();
- }
- } catch (NumberFormatException ex)
- {
- System.err.println("Invalid Integer Argument "+ex);
- }
- if (values[7] != null)
- try
- { // Load the MIB files
- System.err.println("Loading MIBs: "+values[7]);
- target.loadMibs(values[7]);
- } catch (com.adventnet.snmp.corba.SnmpException ex)
- {
- System.err.println("Error loading MIBs: " + ex.reason );
- }
- // create OID array from command line arguments
- String oids[] = new String[opt.remArgs.length-1];
- for (int i=1;i<opt.remArgs.length;i++) oids[i-1] = opt.remArgs[i];
-
- // Set the OID List on the SnmpTarget instance
- System.out.println( "About to set OID list" );
- target.setObjectIDList(oids);
- System.out.println( "setObjectIDList() called " );
- System.out.println( "About to call SNMP GetBulk" );
- String[][] result = target.snmpGetBulkList(); // do a getbulk request
-
- if (result == null)
- {
- System.err.println("Request failed or timed out." + target.getErrorString() + " n" );
- } else
- { // print the values
- System.out.println("Response received. Values:");
- for (int i=0; i < nonRepeaters; i++ )
- System.out.println( result[i][0] );
- StringBuffer sb = new StringBuffer("nRepeaters:n");
-
- for (int j=0;j<result[0].length;j++) {
- for (int i=nonRepeaters;i<oids.length;i++) {
- sb.append(result[i][j] + " t ");
- }
- sb.append("n");
- }
- System.out.println(sb.toString());
- }
- // All done; destroy the server-side Target object
- factory.destroyTarget( target.getName() );
- } catch (Exception e) {
- System.err.println("Error : "+e.getMessage());
- }
- System.exit(0);
- }
- }