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

SNMP编程

开发平台:

C/C++

  1.  /* $Id: trapreceiver.src,v 1.4.4.6 2009/01/28 12:45:56 prathika Exp $ */
  2.  /*
  3.  * @(#)trapreceiver.java
  4.  * Copyright (c) 1996-2009 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.  * [-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.  * [-n] <contextName>  - The snmpv3 contextName to be used.
  23.  * [-i] <contextID>    - The snmpv3 contextID to be used.
  24.  * [-pp] <privProtocol> - The privacy protocol. Must be accompanied with auth,priv password and authProtocol fields.
  25.  */
  26. import com.adventnet.snmp.beans.*;
  27. import com.adventnet.snmp.snmp2.*;
  28. import com.adventnet.snmp.snmp2.usm.*;
  29. public class trapreceiver {
  30.     private static int MIBS = 0;
  31.     private static int COMMUNITY = 1;
  32.     private static int PORT = 2;
  33.     private static int USER_NAME = 3;
  34.     private static int ENGID = 4;
  35.     private static int AUTH_PROTOCOL = 5;
  36.     private static int AUTH_PASSWORD = 6;
  37.     private static int PRIV_PASSWORD = 7;
  38.     private static int DEBUG = 8;
  39.     private static int CONTEXT_NAME = 9;
  40.     private static int CONTEXT_ID = 10;
  41.     private static int PRIV_PROTOCOL = 11;
  42.     public static void main(String args[]) {
  43.     // Take care of getting options
  44.     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] [-n contextName] [-i contextId] [ -pp priv_protocol(DES/AES-128/AES-192/AES-256/3DES) ]";
  45.     String options[] = { "-m" , "-c", "-p", "-u", "-e", "-a", "-w", "-s", "-d", "-n", "-i", "-pp" };
  46.     String values[] = { null, null, null, null, null, null, null, null, "None", null, null, null};
  47.     String userName = null;
  48.     int authProtocol = USMUserEntry.NO_AUTH;
  49.     String authPassword = new String ("");
  50.     String privPassword = new String ("");
  51.     String engineID = null;
  52.     int privProtocol = USMUserEntry.NO_PRIV;
  53.     byte secLevel = 0;
  54.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  55.     if (opt.remArgs.length!=0) opt.usage_error();
  56.     // instantiate a receiver object
  57.     SnmpTrapReceiver receiver = new SnmpTrapReceiver();
  58.  //To load MIBs from compiled file
  59.  receiver.getMibOperations().setLoadFromCompiledMibs(true);
  60.         if (values[COMMUNITY] != null) receiver.setCommunity( values[COMMUNITY] );
  61.     try {  // set trap port to listen on if specified - else port 162
  62.         
  63.         if (values[PORT] != null) 
  64.         receiver.setPort( Integer.parseInt(values[PORT]) );
  65.         else
  66.             receiver.setPort( 162 );
  67.         if (values[USER_NAME] != null) {
  68.             userName = values[USER_NAME];
  69.             receiver.setPrincipal(userName); 
  70.         }
  71.     
  72.         if (values[ENGID] != null) {
  73.             engineID = values[ENGID];
  74. if(engineID.startsWith("0x") || engineID.startsWith("0X"))
  75. engineID = new String(gethexValue(values[ENGID]));
  76.         }
  77.     
  78.         if (values[AUTH_PROTOCOL] != null) {
  79.          if(engineID == null) {
  80.          System.out.println("EngineID is missing");        
  81.          opt.usage_error();
  82.          }         
  83. if ( values[AUTH_PROTOCOL].equals("SHA"))
  84.     authProtocol = USMUserEntry.SHA_AUTH;
  85. else if ( values[AUTH_PROTOCOL].equals("MD5"))
  86.     authProtocol = USMUserEntry.MD5_AUTH;
  87. else
  88.     authProtocol = USMUserEntry.NO_AUTH;
  89. receiver.setAuthProtocol(authProtocol);
  90. receiver.setTrapAuthEnable(true);
  91. secLevel |= 0x01;
  92.         }
  93.         if (values[AUTH_PASSWORD] != null) {
  94.          if(engineID == null) {
  95.          System.out.println("EngineID is missing");        
  96.          opt.usage_error();
  97.          }         
  98. if (secLevel == 0x01) {
  99.     authPassword = values[AUTH_PASSWORD];
  100.     receiver.setAuthPassword(authPassword);
  101. }
  102. else
  103.     opt.usage_error();
  104.         }
  105.         if(values[PRIV_PASSWORD] != null) {
  106.          if(engineID == null) {
  107.          System.out.println("EngineID is missing");        
  108.          opt.usage_error();
  109.          }         
  110. if (secLevel == 0x01)
  111. {
  112.     privPassword = values[PRIV_PASSWORD];
  113.       if(values[PRIV_PROTOCOL] != null)
  114.       {
  115.               if(values[PRIV_PROTOCOL].equals("AES-128"))
  116.                     {
  117.                        privProtocol = USMUserEntry.CFB_AES_128;
  118.                     }
  119.     else if(values[PRIV_PROTOCOL].equals("AES-192"))
  120.                     {
  121.                        privProtocol = USMUserEntry.CFB_AES_192;
  122.                     }
  123.     else if(values[PRIV_PROTOCOL].equals("AES-256"))
  124.                     {
  125.                        privProtocol = USMUserEntry.CFB_AES_256;
  126.                     }
  127.     else if(values[PRIV_PROTOCOL].equals("3DES"))
  128.                     {
  129.                        privProtocol = USMUserEntry.CBC_3DES;
  130.                     }
  131.               else if(values[PRIV_PROTOCOL].equals("DES"))
  132.               {
  133.                       privProtocol = USMUserEntry.CBC_DES;
  134.               }
  135.               else
  136.               {
  137.                       System.out.println(" Invalid PrivProtocol "+values[PRIV_PROTOCOL]);
  138.                       opt.usage_error();
  139.               }
  140.       }
  141.     receiver.setPrivPassword(privPassword);
  142.       receiver.setPrivProtocol(privProtocol);
  143.       if(values[CONTEXT_NAME]!= null)
  144.       receiver.setContextName(values[CONTEXT_NAME]);
  145.       if(values[CONTEXT_ID]!= null)
  146.       receiver.setContextID((values[CONTEXT_ID]).getBytes());
  147.     secLevel |= 0x02;
  148. }
  149. else
  150.     opt.usage_error();
  151.         }
  152.          
  153.     } catch (NumberFormatException ex) {
  154.         System.err.println("Invalid Integer Arg");
  155.     }
  156. if(values[DEBUG].equals("Set"))
  157. receiver.setDebug(true);
  158.     
  159.     if(userName != null) 
  160.         receiver.createUserEntry(engineID.getBytes(),secLevel);
  161.         
  162.     if (values[MIBS] != null) try { // load MIB files
  163.         System.err.println("Loading MIBs: "+values[MIBS]);
  164.         receiver.loadMibs(values[MIBS]);
  165.         System.err.println("Done.");
  166.     } catch (Exception ex) {
  167.         System.err.println("Error loading MIBs: "+ex);
  168.     }
  169.     // we need to instantiate a trap listener to listen for trap events
  170.     TrapListener listener = new TrapListener() {
  171.         // This method is called when trap is received by SnmpTrapReceiver
  172.         public void receivedTrap(TrapEvent trap) {
  173.         System.out.println("Got a trap from: "+trap.getRemoteHost());
  174.             // print PDU details
  175.         System.out.println( ((SnmpTrapReceiver)trap.getSource())
  176.             .getMibOperations().toString(trap.getTrapPDU()) );
  177. if( trap.getTrapPDU().getCommand() == SnmpAPI.TRP_REQ_MSG)
  178. {
  179.         com.adventnet.snmp.mibs.MibTrap trapDefn = // get trap defn
  180.             trap.getTrapDefinition();
  181.         if (trapDefn != null)  // print name and description
  182.             System.out.println("Trap Name: "+trapDefn.getName()+
  183.                        "nDescr: "+trapDefn.getDescription());
  184.          }
  185.          else if( trap.getTrapPDU().getCommand() == SnmpAPI.TRP2_REQ_MSG)
  186. {
  187. com.adventnet.snmp.mibs.MibNode notification = trap.getNotificationDefinition();
  188. if(notification != null)
  189. System.out.println("Notification Name: "+notification.getLabel()+
  190.    "nObjects: "+ notification.getObjects()+
  191.    "nStatus: "+ notification.getStatus()+
  192.    "nDescr: "+notification.getDescription()+
  193.    "nParent: "+ notification.getParent());
  194.         }
  195.         }
  196.     };
  197.  
  198.     receiver.addTrapListener(listener);    
  199.     
  200.     System.out.println("Trap Receiver started at port "+receiver.getPort());
  201.     }
  202.     private static byte[] gethexValue(String value)
  203.     {
  204.         byte temp;
  205.         byte[] Key=new byte[value.length()/2 - 1];
  206.         String ss,str;
  207.         ss = value.substring(2);
  208.         for(int i = 0; i < ss.length(); i+=2)
  209.         {
  210.             str = ss.substring(i,i+2);
  211.             temp = (byte)Integer.parseInt(str,16);
  212.             Key[i/2] = temp;
  213.         }
  214.         return Key;    
  215.     }
  216. }