corbatrapreceiver.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:8k
- /* $Id: corbatrapreceiver.src,v 1.3 2002/09/09 05:45:01 tonyjpaul Exp $ */
- /*
- * @(#)corbatrapreceiver.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * Use CORBA to receive traps from a remote server that forwards traps
- * in CORBA calls. The server can be serving multiple clients with
- * traps as they come in.
- *
- * The CORBA server is specified with the -SnmpServer option
- * and defaults to localhost if not specified.
- *
- * Receive traps and print incoming traps. Loads MIBs
- * as specified, and converts to/from names for loaded MIB data.
- * It also prints loaded trap names and descriptions when the
- * corresponding traps are received.
- * Once a MIB is loaded on the server, susequent uses of
- * This program will have the MIB available for SNMP operations.
- *
- * java corbatrapreceiver [options]
- *
- *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v1/v2c request:
- * java corbatrapreceiver [-SnmpServer hostname] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-m MIB_files] [-c community] [-p port]
- * e.g.
- * java corbatrapreceiver -p 2000
- *
- *<img SRC="images/v3only.jpg" ALT="v3 only"> v3 request:
- * java corbatrapreceiver [-SnmpServer hostname] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-m MIB_files] [-c community] [-p port] [-u user] [-e engineID] [-a authProtocol] [-w auth_password] [-s priv_password]
- * e.g.
- * java corbatrapreceiver -a MD5 -w passwd -u username -p 2000 -c public -m ....mibsrfc1213-mib -e engineid
- *
- * 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.
- * [-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"> [-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.
- * [-e] <engineid> - The engineid to be used for the v3 pdu.
- */
- import org.omg.CosNaming.*;
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import org.omg.CORBA.*;
- import com.adventnet.snmp.corba.*;
- import ParseOptions;
- public class corbatrapreceiver
- {
- static com.adventnet.snmp.corba.SnmpTrapReceiver receiver = null;
- public static void main(String args[]) {
- // Take care of getting options
- String usage = "corbatrapreceiver [-SnmpServer hostname] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-m MIB_files] [-c community] [-p port] [-u user] [-e engineID] [-a authProtocol] [-w auth_password] [-s priv_password]";
- String options[] = { "-SnmpServer", "-ORBInitialPort", "-ORBInitialHost", "-m", "-c", "-p", "-u", "-e", "-a", "-w", "-s" };
- String values[] = { null, null, null, null, null, null, null, null, null, null, null };
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length!=0) opt.usage_error();
- com.adventnet.snmp.corba.SnmpFactory factory = null;
- 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" );
- factory = com.adventnet.snmp.corba.SnmpFactoryHelper.narrow( ncRef.resolve( path ) );
- System.out.println( "Bound to the Factory object" );
- // Get a Receiver object
- receiver = factory.createTrapReceiver();
- String userName = null;
- int authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.NO_AUTH;
- String authPassword = new String ("");
- String privPassword = new String ("");
- String engineID = null;
- int privProtocol = 0;
- byte secLevel = 0;
- final int USM_SECURITY_MODEL = 3;
- //instantiate a receiver object
-
- if (values[4] != null) receiver.setCommunity( values[4] );
- try { // set trap port to listen on if specified - else port 162
-
- if (values[5] != null)
- receiver.setPort( Integer.parseInt(values[5]) );
- else
- receiver.setPort( 162 );
- if (values[6] != null) {
- userName = values[6];
- receiver.setPrincipal(userName);
- }
-
- if (values[7] != null) {
- engineID = values[7];
- //receiver.setEngineID(engineID);
- }
-
- if (values[8] != null) {
- if ( values[8].equals("SHA"))
- authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.SHA_AUTH;
- else if ( values[8].equals("MD5"))
- authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.MD5_AUTH;
- else
- authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.NO_AUTH;
- receiver.setAuthProtocol(authProtocol);
- secLevel |= 0x01;
- }
- if (values[9] != null) {
- if (secLevel == 0x01){
- authPassword = values[9];
- receiver.setAuthPassword(authPassword);
- }
- else
- opt.usage_error();
- }
- if(values[10] != null) {
- if (secLevel == 0x01)
- {
- privPassword = values[10];
- privProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.CBC_DES;
- //receiver.setPrivProtocol(privProtocol);
- receiver.setPrivPassword(privPassword);
- secLevel |= 0x02;
- }
- else
- opt.usage_error();
- }
- //receiver.setSecurityLevel(secLevel);
- } catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Arg");
- }
- if(userName != null) {
- receiver.createUserEntry(engineID.getBytes(),secLevel);
- }
-
- if (values[3] != null) try { // load MIB files
- System.err.println("Loading MIBs: "+values[3]);
- receiver.loadMibs(values[3]);
- } catch (Exception ex) {
- System.err.println("Error loading MIBs: "+ex);
- }
- // we need to instantiate a trap listener to listen for trap events
- TrapListener listener = new TrapListenerImpl();
- orb.connect( listener );
- receiver.addTrapListener(listener);
-
- java.lang.Object sync = new java.lang.Object();
- System.out.println( "Ready to receive traps ... " );
- synchronized ( sync )
- {
- sync.wait();
- }
- } catch (Exception e) {
- System.err.println("Error Receiving Trap: "+e.getMessage());
- }
- }
- }