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

SNMP编程

开发平台:

C/C++

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