rmitrapreceiver.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:7k
- /* $Id: rmitrapreceiver.src,v 1.3 2002/09/09 05:41:24 tonyjpaul Exp $ */
- /*
- * @(#)rmitrapreceiver.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- *
- */
- /**
- * Use RMI to receive traps from a remote server that forwards traps
- * in RMI calls. The server can be serving multiple clients with
- * traps as they come in.
- *
- * The RMI 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 rmitrapreceiver [options]
- *
- *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v1/v2c request:
- * java rmitrapreceiver [-SnmpServer hostname] [-m MIB_files] [-c community] [-p port]
- * e.g.
- * java rmitrapreceiver -p 2000
- *
- *<img SRC="images/v3only.jpg" ALT="v3 only"> v3 request:
- * java rmitrapreceiver [-SnmpServer hostname] [-m MIB_files] [-c community] [-p port] [-u user] [-e engineID] [-a authProtocol] [-w auth_password] [-s priv_password]
- * e.g.
- * java rmitrapreceiver -v v3 -a MD5 -w passwd -u username -p 2000 -c public -m ....mibsrfc1213-mib -e engineid
- *
- * Options:
- * [-SnmpServer ] <hostName> - SnmpServer SERVER option to specify the RMI server host.
- * [-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 com.adventnet.snmp.rmi.*;
- import java.rmi.*;
- public class rmitrapreceiver implements TrapListener {
- static com.adventnet.snmp.rmi.SnmpTrapReceiver receiver = null;
- static final int USM_SECURITY_MODEL = 3;
- public static void main(String args[]) {
- // Take care of getting options
- String usage = " rmitrapreceiver [-SnmpServer hostname] [-m MIB_files] [-c community] [-p port] [-u user] [-e engineID] [-a authProtocol] [-w auth_password] [-s priv_password]";
- String options[] = { "-SnmpServer", "-m", "-c", "-p", "-u", "-e", "-a", "-w", "-s" };
- String values[] = { 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.rmi.SnmpFactory factory = null;
- try {
- System.out.println( "Performing lookup with RMI Registry ... " );
- if(values[0] != null)
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "rmi://" + values[0] + "/AdventnetSnmpFactory" );
- else
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "///AdventnetSnmpFactory" );
- System.out.println( "Lookup succeeded " );
- // Get a Target 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;
- if (values[2] != null) receiver.setCommunity( values[2] );
- try { // set trap port to listen on if specified - else port 162
- if (values[3] != null)
- receiver.setPort( Integer.parseInt(values[3]) );
- else
- receiver.setPort( 162);
- if (values[4] != null) {
- userName = values[4];
- receiver.setPrincipal(userName);
- }
-
- if (values[5] != null) {
- engineID = values[5];
- //receiver.setEngineID(engineID);
- }
-
- if (values[6] != null) {
- if ( values[6].equals("SHA"))
- authProtocol = com.adventnet.snmp.snmp2.usm.USMUserEntry.SHA_AUTH;
- else if ( values[6].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[7] != null) {
- if (secLevel == 0x01){
- authPassword = values[7];
- receiver.setAuthPassword(authPassword);
- }
- else
- opt.usage_error();
- }
- if(values[8] != null) {
- if (secLevel == 0x01)
- {
- privPassword = values[8];
- 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[1] != null) try { // load MIB files
- System.err.println("Loading MIBs: "+values[1]);
- receiver.loadMibs(values[1]);
- } 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 rmitrapreceiver();
- java.rmi.server.UnicastRemoteObject.exportObject(listener);
- receiver.addTrapListener(listener);
- } catch (Exception e) {
- System.err.println("Error Receiving Trap: "+e.getMessage());
- }
- }
- // This method is called when trap is received by SnmpTrapReceiver
- public void receivedTrap(com.adventnet.snmp.beans.TrapEvent trap) throws java.rmi.RemoteException {
- System.out.println("Got a trap from: "+trap.getRemoteHost());
- // print PDU details
- try {
- System.out.println( receiver.getMibOperations()
- .toString(trap.getTrapPDU()) );
- System.out.println( " Opts: "+trap.getTrapPDU().getEnterprise()
- +" - " + trap.getTrapPDU().getTrapType()
- +" - " + trap.getTrapPDU().getSpecificType() );
- // get trap defn
- com.adventnet.snmp.mibs.MibTrap trapDefn = receiver
- .getMibOperations().getMibTrap
- ( trap.getTrapPDU().getEnterprise(),
- trap.getTrapPDU().getTrapType(),
- trap.getTrapPDU().getSpecificType() );
-
- if (trapDefn != null) // print name and description
- System.out.println("Trap Name: "+trapDefn.getName()
- +"nDescr: "+trapDefn.getDescription());
- } catch (Exception ex) {
- System.err.println( "Error: "+ex);
- }
- }
- }