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

SNMP编程

开发平台:

C/C++

  1. /* $Id: rmiwalk.src,v 1.3 2002/09/09 05:41:24 tonyjpaul Exp $ */
  2. /*
  3.  * @(#)rmiwalk.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  Use RMI to do an SNMP walk 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.  *  Multiple OIDs are ok, but checking for when to stop
  14.  *  is based on first OID. 
  15.  *
  16.  *  Once a MIB is loaded on the server, susequent uses of
  17.  *  this program will have the MIB available for SNMP operations.
  18.  *    
  19.  *
  20.  * java rmiwalk [options] host oid ...
  21.  *
  22.  *<img SRC="images/v3only.jpg" ALT="v3 only"> v3 request:
  23.  * java rmiwalk [-SnmpServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName] host OID [OID] ...
  24.  * e.g. 
  25.  * java rmiwalk -v v3 -m ....mibsrfc1213-MIB -u user -a MD5 -w passwd localhost 1.2
  26.  *
  27.  *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v2c request:
  28.  * java rmiwalk [-SnmpServer hostName] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  29.  * e.g. 
  30.  * java rmiwalk -v v2 -m ....mibsrfc1213-MIB localhost 1.2
  31.  *
  32.  * v1 request:
  33.  * java rmiwalk [-SnmpServer hostName] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  34.  * e.g. 
  35.  * java rmiwalk -m ....mibsrfc1213-MIB localhost 1.2
  36.  *
  37.  * Options:
  38.  * [-SnmpServer ] <hostName>      - SnmpServer SERVER option to specify the RMI server host.
  39.  * [-c] <community>    - community String. By default "public".
  40.  * [-p] <port>         - remote port no. By default 161.
  41.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  42.  * [-r] <Retries>      - Retries. By default 0. 
  43.  * [-m] <MIBfile>      - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.       
  44.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
  45.  * [-u] <username>     - The v3 principal/userName
  46.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  47.  * [-w] <authPassword> - The authentication password.
  48.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  49.  * [-i] <contextID>    - The contextID to be used for the v3 pdu.
  50.  * [-n] <contextName>  - The context to be used for the v3 pdu.
  51.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  52.  * OID  Mandatory      - Give multiple no. of Object Identifiers.
  53.  **/
  54. import java.rmi.*;
  55. import com.adventnet.snmp.snmp2.*;
  56. public class rmiwalk {
  57. static final int USM_SECURITY_MODEL = 3;
  58.     public static void main(String args[]) {
  59.     // Take care of getting options
  60.         String usage = "rmiwalk [-SnmpServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName]  host OID [OID] ...";
  61.         String options[] = { "-SnmpServer", "-c", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s" ,"-i","-n"};
  62.         String values[] = { null, null, null, null, null, null, null, null,null,null,null,null,null};
  63.         String userName = new String("");
  64.         String authProtocol = new String("NO_AUTH");
  65.         String authPassword = new String ("");
  66.         String privPassword = new String ("");
  67.         String contextName = new String ("");
  68.         String contextID = new String ("");
  69.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  70.     // check for at least hostname and one OID in remaining arguments
  71.     if (opt.remArgs.length<2) opt.usage_error();
  72.     com.adventnet.snmp.rmi.SnmpFactory factory = null;
  73.     com.adventnet.snmp.rmi.SnmpTarget target = null;
  74.     try {
  75.         System.out.println( "Performing lookup with RMI Registry ... " );
  76.         if(values[0] != null)
  77.         factory = (com.adventnet.snmp.rmi.SnmpFactory) 
  78.             Naming.lookup( "rmi://" + values[0] + "/AdventnetSnmpFactory" );
  79.         else 
  80.         factory = (com.adventnet.snmp.rmi.SnmpFactory) 
  81.             Naming.lookup( "rmi:///AdventnetSnmpFactory" );
  82.         System.out.println( "Lookup succeeded " );
  83.         // Get a Target object
  84.         target = factory.createTarget();
  85.         if(values[6] != null) {  // if SNMP version is specified, set it
  86.         if(values[6].equals("v1"))
  87.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION1 ) ;
  88.         else if(values[6].equals("v2"))
  89.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION2C );
  90.         else if(values[6].equals("v3"))
  91.             target.setSnmpVersion( com.adventnet.snmp.beans.SnmpTarget.VERSION3 );
  92.         else {
  93.             System.out.println("Invalid Version Number");
  94.             System.exit(1);
  95.         }
  96.         }
  97.         target.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  98.         
  99.         if (values[1] != null) // set the community if specified
  100.         target.setCommunity( values[1] );
  101.         try { // set the timeout/retries/port parameters, if specified
  102.         if (values[2] != null)
  103.             target.setTargetPort( Integer.parseInt(values[2]) );
  104.         if (values[3] != null)
  105.             target.setRetries( Integer.parseInt(values[3]) );
  106.         if (values[4] != null)
  107.             target.setTimeout( Integer.parseInt(values[4]) );
  108.         if (values[7] != null)
  109.         userName = values[7];
  110.         if (values[8] != null) {
  111.         System.out.println("authProtocol = " + authProtocol);
  112.         authProtocol = values[8];
  113.         }
  114.         if (values[9] != null)
  115.         authPassword = values[9];
  116.         if (values[10] != null) {
  117.         privPassword = values[10];
  118.         }
  119.         if (values[11] != null) 
  120.   contextID = values[11];
  121.         if (values[12] != null) 
  122.   contextName = values[12];
  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. // Create the SNMPv3 tables.
  137.   target.create_v3_tables();
  138. }
  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.         
  147.         // create OID array from command line arguments
  148.         String oids[] = new String[opt.remArgs.length-1];
  149.         for (int i=1;i<opt.remArgs.length;i++) oids[i-1] = opt.remArgs[i];
  150.     
  151.         // Set the OID List on the SnmpTarget instance
  152.         target.setObjectIDList(oids);
  153.         int maxtry = 0;
  154.         SnmpOID rootoid = target.getSnmpOID();
  155.         while ( maxtry++ < 1000) {  // limit the max getnexts to 1000
  156.         String result[] = target.snmpGetNextList();
  157.         if (result == null) break;
  158.         if ( !com.adventnet.snmp.beans.SnmpTarget.isInSubTree(rootoid,target.getSnmpOID() ) )
  159.             break;  // check first column
  160.         if (maxtry == 1) System.out.println("Response received: ");
  161.         for (int i=0;i<result.length;i++) {  // print the values
  162.             System.out.println(target.getObjectID(i) + ": " + result[i]);
  163.         }
  164.         }
  165.         if (maxtry == 1) {  // we did not get a valid row
  166.         System.err.println("Request failed, timed out or no available data. n"+
  167.                    target.getErrorString());
  168.         } 
  169.     }  catch (Exception e) {
  170.         System.err.println("Error : "+e.getMessage());
  171.     }
  172.     System.exit(0);
  173.     }
  174. }