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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpgetnext.src,v 1.4 2002/09/09 05:41:02 parasuraman Exp $ */
  2. /*
  3.  * @(#)snmpgetnext.java
  4.  * Copyright (c) 1996-2003 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 GETNEXT using com.adventnet.snmp.snmp2 package of
  10.  * AdventNetSNMP2 api.
  11.  * The user could run this application by giving any one of the following usage.
  12.  *  
  13.  * java snmpgetnext [options] hostname oid ...
  14.  *
  15.  * v1 request: 
  16.  * java snmpgetnext [-d] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  17.  * e.g. 
  18.  * java snmpgetnext -p 161 -c public adventnet 1.1.0 1.2.0
  19.  *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only">
  20.  * v2c request: 
  21.  * java snmpgetnext [-d] [-v version(v1,v2)] [-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 snmpgetnext -p 161 -v v2 -c public adventnet 1.1.0 1.2.0
  24.  *<img SRC="images/v3only.jpg" ALT="v3 only"> 
  25.  * v3 request:
  26.  * java snmpgetnext [-d] [-v version(v1,v2,v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] host OID [OID] ...
  27.  * e.g.
  28.  * java snmpgetnext -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.  * Options:
  35.  * [-d]                - Debug output. By default off.
  36.  * [-c] <community>    - community String. By default "public".
  37.  * [-p] <port>         - remote port no. By default 161.
  38.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  39.  * [-r] <Retries>      - Retries. By default 0.      
  40.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
  41.  * [-u] <username>     - The v3 principal/userName
  42.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  43.  * [-w] <authPassword> - The authentication password.
  44.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  45.  * [-n] <contextName>  - The contextName to be used for the v3 pdu.
  46.  * [-i] <contextID>    - The contextID to be used for the v3 pdu.
  47.  * <host> Mandatory    - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  48.  * <OID>  Mandatory    - Give multiple no. of Object Identifiers.
  49.  */
  50. import java.lang.*;
  51. import java.util.*;
  52. import java.net.*;
  53. import com.adventnet.snmp.snmp2.*;
  54. import com.adventnet.snmp.snmp2.usm.*;
  55. public class snmpgetnext {
  56.     public static void main(String args[]) {
  57.         
  58.         // Take care of getting options
  59.         String usage = "snmpgetnext [-d] [-v version(v1,v2,v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] host OID [OID] ...";
  60.         String options[] = { "-d", "-c",  "-wc", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s", "-n", "-i"};
  61.         String values[] = { "None", null, null, null, null, null, "None", null, null, null, null, null, null, null };
  62.         ParseOptions opt = new ParseOptions(args,options,values, usage);
  63.         if (opt.remArgs.length<2) opt.usage_error();
  64.         
  65.         // Start SNMP API
  66.         SnmpAPI api;
  67.         api = new SnmpAPI();
  68.         if (values[0].equals("Set")) api.setDebug( true );    
  69.     
  70.         // Open session 
  71.         SnmpSession session = new SnmpSession(api);
  72. session.setProtocol(session.TRANSPORT_PROVIDER);
  73.         //set the values
  74.         SetValues setVal = new SetValues(session, values);
  75.         if(setVal.usage_error) opt.usage_error(); 
  76.         
  77. // Options that will be used for communication. This can be modified by the user 
  78. // according to his need.
  79. ProtocolOptions params = null;
  80. if(values[3] != null) {
  81. params = new TcpProtocolOptionsImpl(opt.remArgs[0], Integer.parseInt( values[3] ), -1);
  82. }
  83. else {
  84. params = new TcpProtocolOptionsImpl(opt.remArgs[0], 161, -1);
  85. }
  86.         session.setProtocolOptions(params);
  87.         
  88.         // Build GetNext request PDU
  89.         SnmpPDU pdu = new SnmpPDU();
  90.         pdu.setCommand( api.GETNEXT_REQ_MSG );
  91.         // add OIDs
  92.         for (int i=1;i<opt.remArgs.length;i++) {
  93.             SnmpOID oid = new SnmpOID(opt.remArgs[i]);
  94.             if (oid.toValue() == null) 
  95.                 System.err.println("Invalid OID argument: " + opt.remArgs[i]);
  96.             else pdu.addNull(oid);
  97.         }
  98.         try {
  99.             //Open session
  100.             session.open();
  101.         } catch (SnmpException e) {
  102.             System.err.println("Error opening session:"+e.getMessage());
  103.             System.exit(1);
  104.         }
  105.         if(session.getVersion()==SnmpAPI.SNMP_VERSION_3) {
  106. pdu.setUserName(setVal.userName.getBytes());
  107.             USMUtils.init_v3_params(setVal.userName, setVal.authProtocol, setVal.authPassword, setVal.privPassword, params.getSessionId(),session);
  108.             pdu.setContextName(setVal.contextName.getBytes());
  109.             pdu.setContextID(setVal.contextID.getBytes());
  110.         }
  111.         try {
  112.             // Send PDU and receive response PDU
  113.             pdu = session.syncSend(pdu);
  114.         } catch (SnmpException e) {
  115.             System.err.println("Sending PDU"+e.getMessage());
  116.             session.close();
  117.             api.close();             
  118.     System.exit(1);
  119.         }    
  120.         if (pdu == null) {
  121.             // timeout
  122.             System.out.println("Request timed out to: " + opt.remArgs[0] );
  123.             session.close();
  124.             api.close();             
  125.     System.exit(1);
  126.         }
  127.          // print and exit
  128.         System.out.println("Response PDU received from " +((TcpProtocolOptionsImpl)(pdu.getProtocolOptions())).getRemoteHost()+
  129.                ", community: " + pdu.getCommunity());
  130.         // Check for error in response
  131.         if (pdu.getErrstat() != 0)
  132.             System.err.println(pdu.getError());
  133.         else 
  134.             // print the response pdu
  135.             System.out.println(pdu.printVarBinds());
  136.       
  137.         // close session
  138.         session.close();
  139.         // stop api thread
  140.         api.close();
  141.     
  142.         System.exit(0);
  143.     }
  144. }