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

SNMP编程

开发平台:

C/C++

  1. /* $Id: rmigetbulk.src,v 1.3 2002/09/09 05:41:24 tonyjpaul Exp $ */
  2. /*
  3.  * @(#)rmigetbulk.java
  4.  * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  Use RMI to do an SNMP Getbulk based on command line arguments.  
  9.  *  The RMI server is specified with the -SnmpServer option
  10.  *  and defaults to localhost if not specified.
  11.  *
  12.  *  Loads MIBs as specified, and converts to/from names for loaded MIB data. 
  13.  *  Once a MIB is loaded on the server, susequent uses of
  14.  *  this program will have the MIB available for SNMP operations.
  15.  *
  16.  * java rmigetbulk [options] host oid ...
  17.  *
  18.  * v3 request:
  19.  * java rmigetbulk [-SnmpServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-nr non-repeaters] [-mr max-repetitions]  [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName] host OID [OID] ...
  20.  * e.g. 
  21.  * java rmigetbulk -v v3 -m ....mibsrfc1213-MIB -u user -a MD5 -w passwd localhost 1.2.0
  22.  *
  23.  * v2c request:
  24.  * java rmigetbulk [-SnmpServer hostName] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-nr non-repeaters] [-mr max-repetitions] host OID [OID] ...
  25.  * e.g. 
  26.  * java rmigetbulk -v v2 -m ....mibsrfc1213-MIB localhost 1.2.0
  27.  *
  28.  *
  29.  * Options:
  30.  * [-SnmpServer ] <hostName>      - SnmpServer SERVER option to specify the RMI server host.
  31.  * [-c] <community>    - community String. By default "public".
  32.  * [-p] <port>         - remote port no. By default 161.
  33.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  34.  * [-r] <Retries>      - Retries. By default 0. 
  35.  * [-m] <MIBfile>      - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.       
  36.  * [-nr] <non-repeators - value of non-repeators
  37.  * [-mr] <max-repeators - value of max-repeators
  38.  * [-v] <version>      - version(v1 / v2 / v3). By default v1.
  39.  * [-u] <username>     - The v3 principal/userName
  40.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  41.  * [-w] <authPassword> - The authentication password.
  42.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  43.  * [-n] <contextName>  - The context to be used for the v3 pdu.
  44.  * [-i] <contextID>    - The contextID to be used for the v3 pdu.
  45.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  46.  * OID  Mandatory      - Give multiple no. of Object Identifiers.
  47.  **/
  48. import java.rmi.*;
  49. import com.adventnet.snmp.snmp2.*;
  50. public class rmigetbulk {
  51. static final int USM_SECURITY_MODEL = 3;
  52.     public static void main(String args[]) {
  53.     
  54.     // Take care of getting options
  55.     String usage = "rmigetbulk [-SnmpServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-nr non-repeaters] [-mr max-repetitions]  [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName] host OID [OID] ...";
  56.     String options[] = { "-SnmpServer", "-c", "-p", "-r", "-t", "-m", "-nr", "-mr" , "-u", "-a", "-w", "-s", "-v" ,"-i", "-n"};
  57.     String values[] = { null, null, null, null, null, null, null, null,null,null,null,null,null,null,null};
  58.     String userName = new String("");
  59.     String authProtocol = new String("NO_AUTH");
  60.     String authPassword = new String ("");
  61.     String privPassword = new String ("");
  62.     String contextName = new String ("");
  63.     String contextID = new String ("");
  64.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  65.     // check for at least hostname and one OID in remaining arguments
  66.     if (opt.remArgs.length<2) opt.usage_error();
  67.     com.adventnet.snmp.rmi.SnmpFactory factory = null;
  68.     com.adventnet.snmp.rmi.SnmpTarget target = null;
  69.     try {
  70.         System.out.println( "Performing lookup with RMI Registry ... " );
  71.         if(values[0] != null)
  72.         factory = (com.adventnet.snmp.rmi.SnmpFactory) 
  73.             Naming.lookup( "rmi://" + values[0] + "/AdventnetSnmpFactory" );
  74.         else 
  75.         factory = (com.adventnet.snmp.rmi.SnmpFactory) 
  76.             Naming.lookup( "rmi:///AdventnetSnmpFactory" );
  77.         System.out.println( "Lookup succeeded " );
  78.         // Get a Target object
  79.         target = factory.createTarget();
  80.         if(values[12] != null) {  // if SNMP version is specified, set it
  81.         if(values[12].equals("v2"))
  82.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION2C ) ;
  83.         else if(values[12].equals("v1"))
  84.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION1 );
  85.         else if(values[12].equals("v3"))
  86.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION3 );
  87.         else {
  88.             System.out.println("Invalid Version Number");
  89.             System.exit(1);
  90.         }
  91.         }
  92.         int nonRepeaters = 0; // we need this for printing
  93.         target.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  94.         if (values[1] != null) // set the community if specified
  95.         target.setCommunity( values[1] );
  96.         try { // set the timeout/retries/port parameters, if specified
  97.         if (values[2] != null) 
  98.             target.setTargetPort( Integer.parseInt(values[2]) );
  99.         if (values[3] != null) 
  100.             target.setRetries( Integer.parseInt(values[3]) );
  101.         if (values[4] != null) 
  102.             target.setTimeout( Integer.parseInt(values[4]) );
  103.         if (values[6] != null) 
  104.             nonRepeaters = Integer.parseInt(values[6]);
  105.         target.setNonRepeaters( nonRepeaters );
  106.         if (values[7] != null) 
  107.             target.setMaxRepetitions( Integer.parseInt(values[7]) );
  108.             if (values[8] != null)
  109.             userName = values[8];
  110.         if (values[9] != null) {
  111.         System.out.println("authProtocol = " + authProtocol);
  112.         authProtocol = values[9];
  113.         }
  114.         if (values[10] != null)
  115.         authPassword = values[10];
  116.         if (values[11] != null) {
  117.         privPassword = values[11];
  118.         }
  119.   if (values[13] != null)
  120.   contextID = values[13];
  121.   if (values[14] != null)
  122.   contextName = values[14];
  123.         } catch (NumberFormatException ex) {
  124.         System.err.println("Invalid Integer Argument "+ex);
  125.         }
  126.         if(target.getSnmpVersion() == target.VERSION3) {
  127.         target.setPrincipal(userName);
  128.         target.setAuthPassword(authPassword);
  129.         target.setPrivPassword(privPassword);
  130.         if(authProtocol.equals("SHA"))
  131.         target.setAuthProtocol(target.SHA_AUTH);
  132.         else
  133.         target.setAuthProtocol(target.MD5_AUTH);
  134.    target.setContextName(contextName);
  135.   target.setContextID(contextID);
  136.   
  137. // Create the SNMPv3 tables.
  138.   target.create_v3_tables();
  139.         }
  140.         if (values[5] != null) try { // Load the MIB files 
  141.         System.err.println("Loading MIBs: "+values[5]);
  142.         target.loadMibs(values[5]);
  143.         } catch (Exception ex) {
  144.         System.err.println("Error loading MIBs: "+ex);
  145.         }
  146.         // create OID array from command line arguments
  147.         String oids[] = new String[opt.remArgs.length-1];
  148.         for (int i=1;i<opt.remArgs.length;i++) oids[i-1] = opt.remArgs[i];
  149.     
  150.         // Set the OID List on the SnmpTarget instance
  151.         target.setObjectIDList(oids);
  152.         String result[][] = target.snmpGetBulkList(); // do a getbulk request
  153.     
  154.         if (result == null) {
  155.         System.err.println("Request failed or timed out. n"+
  156.                    target.getErrorString());
  157.         } else { // print the values
  158.         System.out.println("Response received.  Values:n");
  159.         for (int i=0;i<nonRepeaters;i++)
  160.             System.out.println(result[i][0]);
  161.         
  162.         StringBuffer sb = new StringBuffer("nRepeaters:n");
  163.         
  164.         for (int j=0;j<result[0].length;j++) {
  165.             for (int i=nonRepeaters;i<oids.length;i++) { 
  166.             sb.append(result[i][j]  + " t ");
  167.             }
  168.             sb.append("n");
  169.         }
  170.         System.out.println(sb.toString());
  171.         }
  172.     }  catch (Exception e) {
  173.         System.err.println("Error : "+e.getMessage());
  174.     }
  175.     System.exit(0);
  176.     }
  177. }