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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpv3getone.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
  2. /*
  3.  * @(#)snmpv3getone.java
  4.  * Copyright (c) 1999 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is a tutorial example program to explain how to write an application to do
  9.  * the basic SNMP operation GET using com.adventnet.snmp.snmp2 package of
  10.  * AdventNetSNMP api.
  11.  *
  12.  * The user could run this application by giving the following usage.
  13.  *  
  14.  * java snmpv3getone hostname OID [OID]
  15.  *
  16.  * where 
  17.  *
  18.  * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
  19.  * OID is the Object Identifier. Multiple OIDs can also be given.
  20.  * The entire OID can be given or it can be given in the form of 1.1.0.
  21.  * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
  22.  * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 .
  23.  *
  24.  * Example usage:
  25.  *
  26.  * java snmpv3getone adventnet 1.1.0 1.2.0 1.3.0 1.4.0 
  27.  *
  28.  */
  29. import java.lang.*;
  30. import java.util.*;
  31. import java.net.*;
  32. import com.adventnet.snmp.snmp2.*;
  33. import com.adventnet.snmp.snmp2.usm.*;
  34. public class snmpv3getone {
  35. public static void main(String args[]) {
  36.     
  37.             if( args.length < 2)
  38. {
  39. System.out.println("Usage : java snmpv3getone hostname OID");
  40. System.exit(0);
  41. }
  42.         
  43. // get the hostname and the OID
  44. String remoteHost = args[0];
  45.         String OID = args[1];       
  46. String userName = "testuser";
  47. int authProtocol = USMUserEntry.MD5_AUTH;
  48. int USM_SECURITY_MODEL = 3;
  49. String authPassword = "test";
  50. String privPassword = "test";
  51. String contextName = "testmachine";
  52. String contextID = "testing";
  53. // Start SNMP API
  54.         SnmpAPI api = new SnmpAPI();
  55. api.setDebug( true );
  56. SecurityProvider provider = api.getSecurityProvider();
  57. provider.registerSecurityModel(3,"com.adventnet.snmp.snmp2.usm.USMUserTable","com.adventnet.snmp.snmp2.usm.USMUserEntry");
  58. try
  59. {
  60. provider.createTable(3);
  61. }
  62. catch(SnmpException e)
  63. {
  64. System.out.println("Exception in creating Security Table for USM");
  65. }
  66. // Open session
  67. SnmpSession session = new SnmpSession(api); 
  68. try 
  69. {
  70.          session.open();
  71.         } 
  72. catch (SnmpException e ) 
  73. {
  74. System.err.println("Error opening socket: "+e);
  75. }
  76. //Build GET Request PDU
  77. SnmpPDU pdu = new SnmpPDU();
  78. //get the value from the command line
  79. UDPProtocolOptions option = new UDPProtocolOptions(remoteHost); 
  80. pdu.setProtocolOptions(option); 
  81. // port number of the remote agent
  82. option.setRemotePort(8001);
  83. // SNMP version
  84. session.setVersion(3);
  85. pdu.setCommand(SnmpAPI.GET_REQ_MSG); 
  86. pdu.setSecurityModel(USM_SECURITY_MODEL);
  87. pdu.setUserName(userName.getBytes());
  88.         USMUtils.init_v3_params(userName, authProtocol, authPassword, privPassword, session.getPeername(), session.getRemotePort(),session);
  89.         pdu.setContextName(contextName.getBytes());
  90.         pdu.setContextID(contextID.getBytes());
  91. // add OIDs
  92. for (int i=1; i<args.length; i++)
  93. {
  94.     SnmpOID oid = new SnmpOID(args[i]);
  95.     pdu.addNull(oid);
  96. }    
  97. try 
  98. {
  99.     // Send PDU and receive response PDU
  100.      SnmpPDU result = session.syncSend(pdu);
  101.          } 
  102. catch (SnmpException e) 
  103. {
  104.      System.err.println("Error sending SNMP request: "+e);
  105. }    
  106. // print the response pdu varbinds
  107. System.out.println(result.printVarBinds());
  108. // close session
  109. session.close();
  110. // close the api thread
  111. api.close();
  112.  }
  113. }