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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpget.src,v 1.4.2.7 2009/01/28 13:01:35 prathika Exp $ */
  2. /*
  3.  * @(#)snmpget.java
  4.  * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is an example program to explain how to write an application to do
  9.  * the basic SNMP operation GET using com.adventnet.snmp.snmp2 and
  10.  * com.adventnet.snmp.mibs packages of AdventNetSNMP2 api.
  11.  * The user could run this application by giving any one of the following usage.
  12.  *  
  13.  * java snmpget [options] hostname oid ...
  14.  *
  15.  * v1 request:
  16.  * java snmpget [-d] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  17.  * e.g. 
  18.  * java snmpget -p 161 -c public -m ../../../mibs/RFC1213-MIB adventnet 1.1.0 1.2.0
  19.  *
  20.  * v2c request:
  21.  * java snmpget [-d] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  22.  * e.g. For v1 request give -v v1 or drop the option -v .
  23.  * java snmpget -p 161 -v v2 -c public -m ../../../mibs/RFC1213-MIB adventnet 1.1.0 1.2.0
  24.  * 
  25.  * v3 request:
  26.  * java snmpget [-d] [-v version(v1,v2,v3)] [-c community] [-m MIB_files] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password][-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)] [-n contextName] [-i contextID] host OID [OID] ...
  27.  * e.g.
  28.  * java snmpget -m ../../../mibs/RFC1213-MIB -v v3 -u initial2 -w initial2Pass -a MD5 10.3.2.120 1.2.0 
  29.  * 
  30.  * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
  31.  * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 . You can also
  32.  * give the entire OID .
  33.  * 
  34.  * If the mib is loaded you can also give string oids in the following formats
  35.  * .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0 or system.sysDescr.0 or
  36.  * sysDescr.0 .
  37.  *
  38.  * Options:
  39.  * [-d]                - Debug output. By default off.
  40.  * [-c] <community>    - community String. By default "public".
  41.  * [-p] <port>         - remote port no. By default 161.
  42.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  43.  * [-r] <Retries>      - Retries. By default 0. 
  44.  * [-m] <MIBfile>      - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.       
  45.  * [-v] <version>      - version(v1 / v2 / v3). By default v1.
  46.  * [-u] <username>     - The v3 principal/userName
  47.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  48.  * [-w] <authPassword> - The authentication password.
  49.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  50.  * [-pp]<privProtocol> - The privacy protocol (DES/AES-128/AES-192/AES-256/3DES).
  51.  * [-n] <contextName>  - The contextName to be used for the v3 pdu.
  52.  * [-i] <contextID>    - The contextID to be used for the v3 pdu.
  53.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  54.  * OID  Mandatory      - Give multiple no. of Object Identifiers.
  55.  */
  56. import java.util.*;
  57. import java.net.*;
  58. import com.adventnet.snmp.snmp2.*;
  59. import com.adventnet.snmp.mibs.*;
  60. import com.adventnet.snmp.snmp2.usm.*;
  61. public class snmpget {
  62.     private static final int DEBUG = 0;
  63.     private static final int MIBS = 6;
  64.     public static void main(String args[]) {
  65.         
  66.     // Take care of getting options     
  67.     String usage = 
  68.         "nsnmpget [-d] [-v version(v1,v2,v3)] [-c community] n"+
  69.         "[-m MIB_files] [-p port] [-r retries] [-t timeout] n"+
  70.         "[-u user] [-a auth_protocol] [-w auth_password] n"+
  71.         "[-s priv_password] [-n contextName] [-i contextID][-pp privProtocol(DES/AES-128/AES-192/AES-256/3DES)]n"+
  72.         "host OID [OID] ...";
  73.     String options[] = 
  74.         { 
  75.             "-d", "-c",  "-wc", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s", "-n", "-i" , "-pp"
  76.         };
  77.     String values[] = 
  78.         { 
  79.             "None", null, null, null, null, null, null, null, null, null, null, null, null, null,null
  80.         };
  81.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  82.     if (opt.remArgs.length<2) opt.usage_error();
  83.     // Start SNMP API
  84.     SnmpAPI api;    
  85.     api = new SnmpAPI();
  86.     if (values[DEBUG].equals("Set")) api.setDebug( true );
  87.     // Open SnmpSession 
  88.     SnmpSession session = new SnmpSession(api);
  89.    int PORT = 3;
  90.         UDPProtocolOptions ses_opt = new UDPProtocolOptions();
  91.         ses_opt.setRemoteHost(opt.remArgs[0]);
  92.         if(values[PORT] != null)
  93.         {
  94.           try
  95.           {
  96.               ses_opt.setRemotePort(Integer.parseInt(values[PORT]));
  97.           }
  98.           catch(Exception exp)
  99.           {
  100.               System.out.println("Invalid port: " + values[PORT]);
  101.               System.exit(1);
  102.           }
  103.         }
  104.         session.setProtocolOptions(ses_opt);
  105.   
  106.     //set the values
  107.     SetValues setVal = new SetValues(session, values);
  108.     if(setVal.usage_error)
  109.     {
  110.         opt.usage_error(); 
  111.     }   
  112.     // Loading MIBS
  113.     MibOperations mibOps = new MibOperations();
  114.      
  115.     // To load MIBs from compiled file
  116.     mibOps.setLoadFromCompiledMibs(true);
  117.     if (values[MIBS] != null)
  118.     {
  119.         try 
  120.         {
  121.             System.err.println("Loading MIBs: "+values[MIBS]);
  122.             mibOps.loadMibModules(values[MIBS]);
  123.         } 
  124.         catch (Exception ex)
  125.         {
  126.             System.err.println("Error loading MIBs: "+ex);
  127.         }
  128.     }
  129.     
  130.     // Build Get request PDU
  131.     SnmpPDU pdu = new SnmpPDU();
  132.     pdu.setCommand( api.GET_REQ_MSG );
  133.     // add OIDs
  134.     for (int i=1;i<opt.remArgs.length;i++) 
  135.     { 
  136.         SnmpOID oid = mibOps.getSnmpOID(opt.remArgs[i]);
  137.         if (oid == null) 
  138.         {
  139.             System.exit(0);
  140.         }
  141.         else
  142.         {
  143.             pdu.addNull(oid);
  144.         }   
  145.     }
  146.     
  147.     try 
  148.     {
  149.         session.open();   // Send PDU
  150.     } 
  151.     catch (SnmpException e) 
  152.     { 
  153.         System.err.println("Error opening seesion");
  154.         System.exit(1);
  155.     }
  156.     
  157.     if(session.getVersion()==SnmpAPI.SNMP_VERSION_3) 
  158.     {
  159.         pdu.setUserName(setVal.userName.getBytes());
  160.         try
  161.         {
  162.             USMUtils.init_v3_parameters(
  163. setVal.userName,
  164. null,
  165. setVal.authProtocol,
  166. setVal.authPassword,
  167. setVal.privPassword,
  168. ses_opt,
  169. session,
  170. false,
  171. setVal.privProtocol);
  172.         }
  173.         catch(Exception e)
  174.         {
  175.             System.out.println(e.getMessage());
  176.         }
  177.         //if(values[11]!=null)
  178.         pdu.setContextName(setVal.contextName.getBytes());
  179.         pdu.setContextID(setVal.contextID.getBytes());
  180.     }
  181.     SnmpPDU res_pdu = null;
  182.     
  183.     try 
  184.     {
  185.         // Send PDU
  186.         res_pdu = session.syncSend(pdu);
  187.     }
  188.     catch (SnmpException e) 
  189.     {
  190.         System.err.println("Sending PDU"+e.getMessage());
  191.         System.exit(1);
  192.     }
  193.     if(res_pdu == null) 
  194.     { 
  195.         // timeout
  196.         System.out.println("Request timed out to: " + opt.remArgs[0] );
  197.         System.exit(1);
  198.     }
  199.     // print and exit
  200.     System.out.println("Response Received from "+ res_pdu.getProtocolOptions().getSessionId());
  201.     // Check for error in response
  202.     if (res_pdu.getErrstat() != 0) 
  203.     {
  204.         System.err.println("Error in response: " + mibOps.getErrorString(res_pdu));
  205.     }
  206.     else 
  207.     {
  208.         // print the response pdu varbinds
  209.         System.out.println(mibOps.varBindsToString(res_pdu));
  210.         // print the response pdu
  211.         System.out.println(mibOps.toString(res_pdu));
  212.     }
  213.     // close session
  214.     session.close();
  215.     // stop api thread
  216.     api.close();
  217.     
  218.     System.exit(0);
  219.     }
  220. }