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

SNMP编程

开发平台:

C/C++

  1.  /* $Id: trapreceiver.src,v 1.4 2002/09/09 05:36:28 tonyjpaul Exp $ */
  2.  /*
  3.  * @(#)trapreceiver.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  *
  7.  */
  8. /** 
  9.  *  Run command line trap receiver and print incoming PDUs.  Loads MIBs 
  10.  *  as specified, and converts to/from names for loaded MIB data. 
  11.  *  It also prints loaded trap names and descriptions when the
  12.  *  corresponding traps are received.
  13.  *
  14.  * [-c] <community>    - community String. By default "public".
  15.  * [-p] <port>         - remote port no. By default 161.
  16.  * [-m] <mibs>           - The mibs to be loaded.
  17.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-e] <engineID>       - The V3 engineID
  18.  * [-u] <username>     - The v3 principal/userName
  19.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  20.  * [-w] <authPassword> - The authentication password.
  21.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  22.  */
  23. import com.adventnet.snmp.beans.*;
  24. import com.adventnet.snmp.snmp2.*;
  25. import com.adventnet.snmp.snmp2.usm.*;
  26. public class trapreceiver {
  27.     private static int MIBS = 0;
  28.     private static int COMMUNITY = 1;
  29.     private static int PORT = 2;
  30.     private static int USER_NAME = 3;
  31.     private static int ENGID = 4;
  32.     private static int AUTH_PROTOCOL = 5;
  33.     private static int AUTH_PASSWORD = 6;
  34.     private static int PRIV_PASSWORD = 7;
  35.     private static int DEBUG = 8;
  36.     public static void main(String args[]) {
  37.     // Take care of getting options
  38.     String usage = "trapreceiver [-m MIB_files] [-c community] [-p port] [-u user] [-e engineID(1234.../0x1234...)] [-a authProtocol(MD5/SHA)] [-w auth_password] [-s priv_password] [-d]";
  39.     String options[] = { "-m" , "-c", "-p", "-u", "-e", "-a", "-w", "-s", "-d" };
  40.     String values[] = { null, null, null, null, null, null, null, null, "None"};
  41.     String userName = null;
  42.     int authProtocol = USMUserEntry.NO_AUTH;
  43.     String authPassword = new String ("");
  44.     String privPassword = new String ("");
  45.     String engineID = null;
  46.     int privProtocol = 0;
  47.     byte secLevel = 0;
  48.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  49.     if (opt.remArgs.length!=0) opt.usage_error();
  50.     // instantiate a receiver object
  51.     SnmpTrapReceiver receiver = new SnmpTrapReceiver();
  52.  //To load MIBs from compiled file
  53.  receiver.getMibOperations().setLoadFromCompiledMibs(true);
  54.         if (values[COMMUNITY] != null) receiver.setCommunity( values[COMMUNITY] );
  55.     try {  // set trap port to listen on if specified - else port 162
  56.         
  57.         if (values[PORT] != null) 
  58.         receiver.setPort( Integer.parseInt(values[PORT]) );
  59.         else
  60.             receiver.setPort( 162 );
  61.         if (values[USER_NAME] != null) {
  62.             userName = values[USER_NAME];
  63.             receiver.setPrincipal(userName); 
  64.         }
  65.     
  66.         if (values[ENGID] != null) {
  67.             engineID = values[ENGID];
  68. if(engineID.startsWith("0x") || engineID.startsWith("0X"))
  69. engineID = new String(gethexValue(values[ENGID]));
  70.         }
  71.     
  72.         if (values[AUTH_PROTOCOL] != null) {
  73.          if(engineID == null) {
  74.          System.out.println("EngineID is missing");        
  75.          opt.usage_error();
  76.          }         
  77. if ( values[AUTH_PROTOCOL].equals("SHA"))
  78.     authProtocol = USMUserEntry.SHA_AUTH;
  79. else if ( values[AUTH_PROTOCOL].equals("MD5"))
  80.     authProtocol = USMUserEntry.MD5_AUTH;
  81. else
  82.     authProtocol = USMUserEntry.NO_AUTH;
  83. receiver.setAuthProtocol(authProtocol);
  84. receiver.setTrapAuthEnable(true);
  85. secLevel |= 0x01;
  86.         }
  87.         if (values[AUTH_PASSWORD] != null) {
  88.          if(engineID == null) {
  89.          System.out.println("EngineID is missing");        
  90.          opt.usage_error();
  91.          }         
  92. if (secLevel == 0x01) {
  93.     authPassword = values[AUTH_PASSWORD];
  94.     receiver.setAuthPassword(authPassword);
  95. }
  96. else
  97.     opt.usage_error();
  98.         }
  99.         if(values[PRIV_PASSWORD] != null) {
  100.          if(engineID == null) {
  101.          System.out.println("EngineID is missing");        
  102.          opt.usage_error();
  103.          }         
  104. if (secLevel == 0x01)
  105. {
  106.     privPassword = values[PRIV_PASSWORD];
  107.     privProtocol = USMUserEntry.CBC_DES;
  108.     receiver.setPrivPassword(privPassword);
  109.     secLevel |= 0x02;
  110. }
  111. else
  112.     opt.usage_error();
  113.         }
  114.          
  115.     } catch (NumberFormatException ex) {
  116.         System.err.println("Invalid Integer Arg");
  117.     }
  118. if(values[DEBUG].equals("Set"))
  119. receiver.setDebug(true);
  120.     
  121.     if(userName != null) 
  122.         receiver.createUserEntry(engineID.getBytes(),secLevel);
  123.         
  124.     if (values[MIBS] != null) try { // load MIB files
  125.         System.err.println("Loading MIBs: "+values[MIBS]);
  126.         receiver.loadMibs(values[MIBS]);
  127.         System.err.println("Done.");
  128.     } catch (Exception ex) {
  129.         System.err.println("Error loading MIBs: "+ex);
  130.     }
  131.     // we need to instantiate a trap listener to listen for trap events
  132.     TrapListener listener = new TrapListener() {
  133.         // This method is called when trap is received by SnmpTrapReceiver
  134.         public void receivedTrap(TrapEvent trap) {
  135.         System.out.println("Got a trap from: "+trap.getRemoteHost());
  136.             // print PDU details
  137.         System.out.println( ((SnmpTrapReceiver)trap.getSource())
  138.             .getMibOperations().toString(trap.getTrapPDU()) );
  139. if( trap.getTrapPDU().getCommand() == SnmpAPI.TRP_REQ_MSG)
  140. {
  141.         com.adventnet.snmp.mibs.MibTrap trapDefn = // get trap defn
  142.             trap.getTrapDefinition();
  143.         if (trapDefn != null)  // print name and description
  144.             System.out.println("Trap Name: "+trapDefn.getName()+
  145.                        "nDescr: "+trapDefn.getDescription());
  146.          }
  147.          else if( trap.getTrapPDU().getCommand() == SnmpAPI.TRP2_REQ_MSG)
  148. {
  149. com.adventnet.snmp.mibs.MibNode notification = trap.getNotificationDefinition();
  150. if(notification != null)
  151. System.out.println("Notification Name: "+notification.getLabel()+
  152.    "nObjects: "+ notification.getObjects()+
  153.    "nStatus: "+ notification.getStatus()+
  154.    "nDescr: "+notification.getDescription()+
  155.    "nParent: "+ notification.getParent());
  156.         }
  157.         }
  158.     };
  159.  
  160.     receiver.addTrapListener(listener);    
  161.     
  162.     System.out.println("Trap Receiver started at port "+receiver.getPort());
  163.     }
  164.     private static byte[] gethexValue(String value)
  165.     {
  166.         byte temp;
  167.         byte[] Key=new byte[value.length()/2 - 1];
  168.         String ss,str;
  169.         ss = value.substring(2);
  170.         for(int i = 0; i < ss.length(); i+=2)
  171.         {
  172.             str = ss.substring(i,i+2);
  173.             temp = (byte)Integer.parseInt(str,16);
  174.             Key[i/2] = temp;
  175.         }
  176.         return Key;    
  177.     }
  178. }