corbatrapreceiver.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:8k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: corbatrapreceiver.src,v 1.3 2002/09/09 05:45:01 tonyjpaul Exp $ */
  2. /*
  3.  * @(#)corbatrapreceiver.java
  4.  * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  *  Use CORBA to receive traps from a remote server that forwards traps
  9.  *  in CORBA calls.  The server can be serving multiple clients with
  10.  *  traps as they come in.
  11.  *
  12.  *  The CORBA server is specified with the -SnmpServer option
  13.  *  and defaults to localhost if not specified.
  14.  *
  15.  *  Receive traps and print incoming traps.  Loads MIBs 
  16.  *  as specified, and converts to/from names for loaded MIB data. 
  17.  *  It also prints loaded trap names and descriptions when the
  18.  *  corresponding traps are received.
  19.  *  Once a MIB is loaded on the server, susequent uses of
  20.  *  This program will have the MIB available for SNMP operations.
  21.  *  
  22.  * java corbatrapreceiver [options] 
  23.  *
  24.  *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v1/v2c request:
  25.  * java corbatrapreceiver [-SnmpServer hostname] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-m MIB_files] [-c community] [-p port] 
  26.  * e.g. 
  27.  * java corbatrapreceiver  -p 2000
  28.  *
  29.  *<img SRC="images/v3only.jpg" ALT="v3 only"> v3 request:
  30.  * 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]
  31.  * e.g. 
  32.  * java corbatrapreceiver -a MD5 -w passwd -u username -p 2000 -c public -m ....mibsrfc1213-mib -e engineid 
  33.  *
  34.  * Options:
  35.  * [-SnmpServer ] <hostName>      - SnmpServer SERVER option to specify the CORBA server host.
  36.  * [-ORBInitialPort ] <orbPort>   - port on which transient name server is running
  37.  * [-ORBInitialHost ] <orbHost>   - host name of transient name server
  38.  * [-c] <community>    - community String. By default "public".
  39.  * [-p] <port>         - remote port no. By default 161.
  40.  * [-m] <MIBfile>      - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.       
  41.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-u] <username>     - The v3 principal/userName
  42.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  43.  * [-w] <authPassword> - The authentication password.
  44.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  45.  * [-e] <engineid>  - The engineid to be used for the v3 pdu.
  46.  */
  47. import org.omg.CosNaming.*;
  48. import java.lang.*;
  49. import java.util.*;
  50. import java.net.*;
  51. import org.omg.CORBA.*;
  52. import com.adventnet.snmp.corba.*;
  53. import ParseOptions;
  54. public class corbatrapreceiver 
  55. {    
  56.     static com.adventnet.snmp.corba.SnmpTrapReceiver receiver = null;
  57.     public static void main(String args[]) {
  58.     // Take care of getting options            
  59.     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]";
  60.     String options[] = { "-SnmpServer", "-ORBInitialPort", "-ORBInitialHost", "-m", "-c", "-p", "-u", "-e", "-a", "-w", "-s" };
  61.     String values[] = { null, null, null, null, null, null, null, null, null, null, null };
  62.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  63.     if (opt.remArgs.length!=0) opt.usage_error();
  64.     com.adventnet.snmp.corba.SnmpFactory factory = null;
  65.     try {
  66.             // Initialise the ORB
  67.             System.out.println( "Initialising the ORB" );
  68.             org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( args, null );
  69.             // Use Naming Service
  70.             org.omg.CORBA.Object objRef = orb.resolve_initial_references( 
  71.                                                                         "NameService" ); 
  72.             NamingContext ncRef = NamingContextHelper.narrow( objRef ); 
  73.             // bind the Object Reference in Naming
  74.             NameComponent nc = new NameComponent( "AdventnetSnmpFactory" , "" );
  75.             NameComponent[] path = { nc };
  76.             
  77.             // Bind to the SnmpTarget object 
  78.             //System.out.println( "Binding to the Factory object" );
  79.             //CORBAsnmp.SnmpTargetFactory factory = CORBAsnmp.SnmpTargetFactoryHelper.
  80.                             //bind( orb, "AdventnetSnmpTargetFactory" );
  81.             factory = com.adventnet.snmp.corba.SnmpFactoryHelper.narrow( ncRef.resolve( path ) );
  82.             System.out.println( "Bound to the Factory object" );
  83.             // Get a Receiver object
  84.             receiver = factory.createTrapReceiver();
  85.             String userName = null;
  86.             int authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.NO_AUTH;
  87.             String authPassword = new String ("");
  88.             String privPassword = new String ("");
  89.             String engineID = null;
  90.             int privProtocol = 0;
  91.             byte secLevel = 0;
  92. final int USM_SECURITY_MODEL = 3;
  93.             //instantiate a receiver object
  94.             
  95.             if (values[4] != null) receiver.setCommunity( values[4] );
  96.             try {  // set trap port to listen on if specified - else port 162
  97.                 
  98.                 if (values[5] != null) 
  99.                     receiver.setPort( Integer.parseInt(values[5]) );
  100.                 else
  101.                     receiver.setPort( 162 );
  102.                 if (values[6] != null) {
  103.                     userName = values[6];
  104.                     receiver.setPrincipal(userName);
  105.                 }
  106.                 
  107.                 if (values[7] != null) {
  108.                     engineID = values[7];
  109.                     //receiver.setEngineID(engineID);
  110.                 }
  111.                 
  112.                 if (values[8] != null) {
  113.                     if ( values[8].equals("SHA"))
  114.                         authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.SHA_AUTH;
  115.                     else if ( values[8].equals("MD5"))
  116.                         authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.MD5_AUTH;
  117.                     else
  118.                         authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.NO_AUTH;
  119.                     receiver.setAuthProtocol(authProtocol);
  120.                     secLevel |= 0x01;
  121.                 }
  122.                 if (values[9] != null) {
  123.                     if (secLevel == 0x01){
  124.                         authPassword = values[9];
  125.                         receiver.setAuthPassword(authPassword);
  126.                     }
  127.                     else
  128.                         opt.usage_error();
  129.                 }
  130.                 if(values[10] != null) {
  131.                     if (secLevel == 0x01)
  132.                     {
  133.                         privPassword = values[10];
  134.                         privProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.CBC_DES;
  135.                         //receiver.setPrivProtocol(privProtocol);
  136.                         receiver.setPrivPassword(privPassword);
  137.                         secLevel |= 0x02;
  138.                     }
  139.                     else
  140.                         opt.usage_error();
  141.                 }
  142.                 //receiver.setSecurityLevel(secLevel);
  143.             } catch (NumberFormatException ex) {
  144.                 System.err.println("Invalid Integer Arg");
  145.             }
  146.             if(userName != null) {
  147.                 receiver.createUserEntry(engineID.getBytes(),secLevel);
  148.             }
  149.             
  150.             if (values[3] != null) try { // load MIB files
  151.                 System.err.println("Loading MIBs: "+values[3]);
  152.                 receiver.loadMibs(values[3]);
  153.             } catch (Exception ex) {
  154.                 System.err.println("Error loading MIBs: "+ex);
  155.             }
  156.         // we need to instantiate a trap listener to listen for trap events
  157.         TrapListener listener = new TrapListenerImpl();
  158.             orb.connect( listener );
  159.         receiver.addTrapListener(listener);    
  160.             
  161.             java.lang.Object sync = new java.lang.Object();
  162.             System.out.println( "Ready to receive traps ... " );
  163.             synchronized ( sync )
  164.             {
  165.                 sync.wait();
  166.             }
  167.     }  catch (Exception e) {
  168.         System.err.println("Error Receiving Trap: "+e.getMessage());
  169.     }
  170.     }
  171. }