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

SNMP编程

开发平台:

C/C++

  1. /*$Id: snmpgetnext.src,v 1.3.2.4 2009/01/28 12:45:56 prathika Exp $*/
  2. /*
  3.  * @(#)snmpgetnext.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 GetNext based on command line arguments.  Loads MIBs 
  9.  *  as specified, and converts to/from names for loaded MIB data. 
  10.  *
  11.  * [-d]                - Debug output. By default off.
  12.  * [-c] <community>    - community String. By default "public".
  13.  * [-p] <port>         - remote port no. By default 161.
  14.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  15.  * [-r] <Retries>      - Retries. By default 0.      
  16.  * [-m] <mibs>           - The mibs to be loaded.
  17.  * [-v] <version>      - version(v1 / v2 / v3). By default v1.
  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.  * <host> Mandatory    - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  26.  * <OID>  Mandatory    - Give multiple no. of Object Identifiers.
  27.  */
  28. import com.adventnet.snmp.beans.*;
  29. public class snmpgetnext {
  30.     private static final int COMMUNITY = 1;
  31.     private static final int PORT = 2;
  32.     private static final int RETRIES = 3;
  33.     private static final int TIMEOUT = 4;
  34.     private static final int MIBS = 5;
  35.     private static final int VERSION = 0;
  36.     private static final int USER_NAME = 6;
  37.     private static final int AUTH_PROTOCOL = 7;
  38.     private static final int AUTH_PASSWORD = 8;
  39.     private static final int PRIV_PASSWORD = 9;
  40. private static final int CONTEXT_NAME = 10;
  41. private static final int CONTEXT_ID = 11;
  42.     private static final int DEBUG = 12;
  43.     private static final int PRIV_PROTOCOL= 13;
  44.     public static void main(String args[]) {
  45.     // Take care of getting options
  46.     String usage = "snmpget [-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 [OID] ...";
  47.     String options[] = { "-v", "-c", "-p", "-r", "-t", "-m" , "-u", "-a", "-w", "-s", "-n", "-i", "-d", "-pp" };
  48.     String values[] = { null, null, null, null, null, null, null, null, null, null, null, null, "None", null};
  49.     String authProtocol = new String("NO_AUTH");
  50.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  51.     // check for at least hostname and one OID in remaining arguments
  52.     if (opt.remArgs.length<2) opt.usage_error();
  53.  if(values[VERSION] != null)
  54.   {      
  55.     if ((values[VERSION].equals("v3")) && (values[USER_NAME] == null)) opt.usage_error();
  56.   }
  57.     // Use an SNMP target bean to perform SNMP operations
  58.     SnmpTarget target = new SnmpTarget();
  59.  //To load MIBs from compiled file
  60.  target.setLoadFromCompiledMibs(true);
  61.     if (values[DEBUG].equals("Set")) target.setDebug(true);
  62.         if(values[VERSION] != null) {  // if SNMP version is specified, set it
  63.             if(values[VERSION].equals("v2"))
  64.                 target.setSnmpVersion( SnmpTarget.VERSION2C ) ;
  65.             else if(values[VERSION].equals("v1"))
  66.                 target.setSnmpVersion( SnmpTarget.VERSION1 );
  67.             else if(values[VERSION].equals("v3"))
  68.                 target.setSnmpVersion( SnmpTarget.VERSION3 );
  69.             else {
  70.                 System.out.println("Invalid Version Number"); 
  71.                 System.exit(1);
  72.             }
  73.         }
  74.     target.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  75.     if (values[COMMUNITY] != null) // set the community if specified
  76.         target.setCommunity( values[COMMUNITY] );
  77.     try { // set the timeout/retries/port parameters, if specified
  78.         if (values[PORT] != null) 
  79.             target.setTargetPort( Integer.parseInt(values[PORT]) );
  80.         if (values[RETRIES] != null) 
  81.             target.setRetries( Integer.parseInt(values[RETRIES]) );
  82.         if (values[TIMEOUT] != null) 
  83.             target.setTimeout( Integer.parseInt(values[TIMEOUT]) );
  84.     } catch (NumberFormatException ex) {
  85.         System.err.println("Invalid Integer Argument "+ex);
  86.     }
  87.     if(target.getSnmpVersion() == target.VERSION3) {
  88.         if (values[USER_NAME] != null) 
  89.             target.setPrincipal(values[USER_NAME]);
  90.         if (values[AUTH_PROTOCOL] != null) {
  91.             //System.out.println("authProtocol = " + authProtocol);
  92.             authProtocol = values[AUTH_PROTOCOL];
  93.         }
  94.         if(authProtocol.equals("SHA"))
  95.             target.setAuthProtocol(target.SHA_AUTH);
  96.         else if(authProtocol.equals("MD5"))
  97.             target.setAuthProtocol(target.MD5_AUTH);
  98.         else
  99.             target.setAuthProtocol(target.NO_AUTH);
  100.         if (values[AUTH_PASSWORD] != null) 
  101.             target.setAuthPassword(values[AUTH_PASSWORD]);
  102.         if (values[PRIV_PASSWORD] != null) 
  103.           {
  104.             target.setPrivPassword(values[PRIV_PASSWORD]);
  105.             if(values[PRIV_PROTOCOL] != null)
  106.             {
  107.                     if(values[PRIV_PROTOCOL].equals("AES-128"))
  108.                     {
  109.                        target.setPrivProtocol(target.CFB_AES_128);
  110.                     }
  111.     else if(values[PRIV_PROTOCOL].equals("AES-192"))
  112.                     {
  113.                        target.setPrivProtocol(target.CFB_AES_192);
  114.                     }
  115.     else if(values[PRIV_PROTOCOL].equals("AES-256"))
  116.                     {
  117.                        target.setPrivProtocol(target.CFB_AES_256);
  118.                     }
  119.     else if(values[PRIV_PROTOCOL].equals("3DES"))
  120.                     {
  121.                        target.setPrivProtocol(target.CBC_3DES);
  122.                     }
  123.                     else if(values[PRIV_PROTOCOL].equals("DES"))
  124.                     {
  125.                        target.setPrivProtocol(target.CBC_DES);
  126.                     }
  127.                     else
  128.                     {
  129.                      System.out.println(" Invalid PrivProtocol "+values[PRIV_PROTOCOL]);
  130.                      opt.usage_error();
  131.                     }
  132.             }
  133.         }
  134. if(values[CONTEXT_NAME]!= null)
  135. target.setContextName(values[CONTEXT_NAME]);
  136. if(values[CONTEXT_ID]!= null)
  137. target.setContextID(values[CONTEXT_ID]);
  138.     }
  139.     if (values[MIBS] != null) try { // Load the MIB files 
  140.         System.err.println("Loading MIBs: "+values[MIBS]);
  141.         target.loadMibs(values[MIBS]);
  142.     } catch (Exception ex) {
  143.         System.err.println("Error loading MIBs: "+ex);
  144.     }
  145. if(target.getSnmpVersion() == target.VERSION3){
  146. target.create_v3_tables();
  147. }
  148.     // create OID array from command line arguments
  149.     String oids[] = new String[opt.remArgs.length-1];
  150.     for (int i=1;i<opt.remArgs.length;i++) oids[i-1] = opt.remArgs[i];
  151.     
  152.     // Set the OID List on the SnmpTarget instance
  153.     target.setObjectIDList(oids);
  154.     String result[] = target.snmpGetNextList(); // do a getnext request
  155.     
  156.     if (result == null) {
  157.         System.err.println("Request failed or timed out. n"+
  158.                    target.getErrorString());
  159.     } else { // print the values
  160.         System.out.println("Response received.  Values:");
  161.         for (int i=0;i<oids.length;i++) { 
  162.             System.out.println(target.getObjectID(i) + ": " + result[i]);
  163.         }
  164.     }
  165.     System.exit(0);
  166.     }
  167. }