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

SNMP编程

开发平台:

C/C++

  1. /*$Id: snmpwalk.src,v 1.4.2.4 2009/01/28 12:45:56 prathika Exp $*/
  2. /*
  3.  * @(#)snmpwalk.java
  4.  * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  Do an SNMP walk based on command line arguments.  Loads MIBs 
  9.  *  as specified, and converts to/from names for loaded MIB data.
  10.  *  Multiple OIDs are ok, but checking for when to stop
  11.  *  is based on first OID. 
  12.  *
  13.  * [-d]                - Debug output. By default off.
  14.  * [-c] <community>    - community String. By default "public".
  15.  * [-p] <port>         - remote port no. By default 161.
  16.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  17.  * [-r] <Retries>      - Retries. By default 0.      
  18.  * [-m] <mibs>           - The mibs to be loaded.
  19.  * [-v] <version>      - version(v1 / v2 / v3). By default v1.
  20.  * [-u] <username>     - The v3 principal/userName
  21.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  22.  * [-w] <authPassword> - The authentication password.
  23.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  24.  * [-n] <contextName>  - The snmpv3 contextName to be used.
  25.  * [-i] <contextID>    - The snmpv3 contextID to be used.
  26.  * [-pp] <privProtocol> - The privacy protocol . Must be accompanied with auth,priv password and authProtocol fields.
  27.  * <host> Mandatory    - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  28.  * <OID>  Mandatory    - Give multiple no. of Object Identifiers.
  29.  */
  30. import com.adventnet.snmp.beans.*;
  31. import com.adventnet.snmp.snmp2.*;
  32. public class snmpwalk {
  33.     
  34.     private static final int COMMUNITY = 1;
  35.     private static final int PORT = 2;
  36.     private static final int RETRIES = 3;
  37.     private static final int TIMEOUT = 4;
  38.     private static final int MIBS = 5;
  39.     private static final int VERSION = 0;
  40.     private static final int USER_NAME = 6;
  41.     private static final int AUTH_PROTOCOL = 7;
  42.     private static final int AUTH_PASSWORD = 8;
  43.     private static final int PRIV_PASSWORD = 9;
  44. private static final int CONTEXT_NAME = 10;
  45. private static final int CONTEXT_ID = 11;
  46.     private static final int DEBUG = 12;
  47.     private static final int PRIV_PROTOCOL = 13;
  48.     public static void main(String args[]) {
  49.         // Take care of getting options
  50.     String usage = "snmpwalk [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol(MD5/SHA)] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] [-d debug] [ -pp priv_protocol (DES/AES-128/AES-192/AES-256/3DES) ] host OID";
  51.     String options[] = { "-v", "-c", "-p", "-r", "-t", "-m" , "-u", "-a", "-w", "-s", "-n", "-i", "-d", "-pp" };
  52.     String values[] = { null, null, null, null, null, null, null, null, null, null, null, null, "None", null};
  53.     String authProtocol = new String("NO_AUTH");
  54.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  55.     // check for at least hostname and one OID in remaining arguments
  56.     if (opt.remArgs.length != 2) opt.usage_error();
  57.  if(values[VERSION] != null)
  58.   {       
  59.   if ((values[VERSION].equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
  60. }
  61.     // Use an SNMP target bean to perform SNMP operations
  62.     SnmpTarget target = new SnmpTarget();
  63.  //To load MIBs from compiled file
  64.  target.setLoadFromCompiledMibs(true);
  65.     if (values[DEBUG].equals("Set")) target.setDebug(true);
  66.         if(values[VERSION] != null) {  // if SNMP version is specified, set it
  67.             if(values[VERSION].equals("v2"))
  68.                 target.setSnmpVersion( SnmpTarget.VERSION2C ) ;
  69.             else if(values[VERSION].equals("v1"))
  70.                 target.setSnmpVersion( SnmpTarget.VERSION1 );
  71.             else if(values[VERSION].equals("v3"))
  72.                 target.setSnmpVersion( SnmpTarget.VERSION3 );
  73.             else {
  74.             System.out.println("Invalid Version Number"); 
  75.             System.exit(1);
  76.             }
  77.         }
  78.     target.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  79.     if (values[COMMUNITY] != null) // set the community if specified
  80.         target.setCommunity( values[COMMUNITY] );
  81.     try { // set the timeout/retries/port parameters, if specified
  82.         if (values[PORT] != null) 
  83.             target.setTargetPort( Integer.parseInt(values[PORT]) );
  84.         if (values[RETRIES] != null) 
  85.             target.setRetries( Integer.parseInt(values[RETRIES]) );
  86.         if (values[TIMEOUT] != null) 
  87.             target.setTimeout( Integer.parseInt(values[TIMEOUT]) );
  88.     } catch (NumberFormatException ex) {
  89.         System.err.println("Invalid Integer Argument "+ex);
  90.     }
  91.     if(target.getSnmpVersion() == target.VERSION3) {
  92.         if (values[USER_NAME] != null) 
  93.             target.setPrincipal(values[USER_NAME]);
  94.         if (values[AUTH_PROTOCOL] != null) {
  95.             //System.out.println("authProtocol = " + authProtocol);
  96.             authProtocol = values[AUTH_PROTOCOL];
  97.         }
  98.         if(authProtocol.equals("SHA"))
  99.             target.setAuthProtocol(target.SHA_AUTH);
  100.         else if(authProtocol.equals("MD5"))
  101.             target.setAuthProtocol(target.MD5_AUTH);
  102.         else
  103.             target.setAuthProtocol(target.NO_AUTH);
  104.         if (values[AUTH_PASSWORD] != null) 
  105.             target.setAuthPassword(values[AUTH_PASSWORD]);
  106.         if (values[PRIV_PASSWORD] != null) 
  107.         {
  108.             target.setPrivPassword(values[PRIV_PASSWORD]);
  109.             if(values[PRIV_PROTOCOL] != null)
  110.             {
  111.                    if(values[PRIV_PROTOCOL].equals("AES-128"))
  112.                     {
  113.                        target.setPrivProtocol(target.CFB_AES_128);
  114.                     }
  115.     else if(values[PRIV_PROTOCOL].equals("AES-192"))
  116.                     {
  117.                        target.setPrivProtocol(target.CFB_AES_192);
  118.                     }
  119.     else if(values[PRIV_PROTOCOL].equals("AES-256"))
  120.                     {
  121.                        target.setPrivProtocol(target.CFB_AES_256);
  122.                     }
  123.     else if(values[PRIV_PROTOCOL].equals("3DES"))
  124.                     {
  125.                        target.setPrivProtocol(target.CBC_3DES);
  126.                     }
  127.                     else if(values[PRIV_PROTOCOL].equals("DES"))
  128.                     {
  129.                        target.setPrivProtocol(target.CBC_DES);
  130.                     }
  131.                     else
  132.                     {
  133.                      System.out.println(" Invalid PrivProtocol "+values[PRIV_PROTOCOL]);
  134.                      opt.usage_error();
  135.                     }
  136.             }
  137.         }
  138. if(values[CONTEXT_NAME]!= null)
  139. target.setContextName(values[CONTEXT_NAME]);
  140. if(values[CONTEXT_ID]!= null)
  141. target.setContextID(values[CONTEXT_ID]);
  142.     }
  143.     if (values[MIBS] != null) try { // Load the MIB files 
  144.         System.err.println("Loading MIBs: "+values[MIBS]);
  145.         target.loadMibs(values[MIBS]);
  146.     } catch (Exception ex) {
  147.         System.err.println("Error loading MIBs: "+ex);
  148.     }
  149. if(target.getSnmpVersion() == target.VERSION3){
  150. target.create_v3_tables();
  151. }
  152.     // Set the OID on the SnmpTarget instance
  153.     target.setObjectID(opt.remArgs[1]);
  154.     int maxtry = 0;
  155.     SnmpOID[] oidList = target.getSnmpOIDList();
  156.     if(oidList == null) {
  157.      System.out.println("Invalid OID has been specified / Check if the OID is present in the MIB loaded if any");
  158.      target.releaseResources();
  159.     }
  160.     else {
  161.     SnmpOID rootoid = oidList[0];
  162.     while ( maxtry++ < 1000) {  // limit the max getnexts to 1000
  163. String result[] = target.snmpGetNextList();
  164. if (result == null) break;
  165. if ( !SnmpTarget.isInSubTree(rootoid,target.getSnmpOID()) )
  166.   break;  // check first column
  167. if (maxtry == 1) System.out.println("Response received: ");
  168. for (int i=0;i<result.length;i++) {  // print the values
  169.     System.out.println(target.getObjectID(i) + ": " + result[i]);
  170. }
  171.     }
  172.     if (maxtry == 1) {  // we did not get a valid row
  173. System.err.println("Request failed, timed out or no available data. n"+
  174.    target.getErrorString());
  175.     } 
  176.     target.releaseResources();
  177. }
  178.     }
  179. }