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

SNMP编程

开发平台:

C/C++

  1. * @(#)GetRowData.java
  2.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  3.  * Please read the associated COPYRIGHTS file for more details.
  4.  */
  5. /**
  6.  * This is an example program to explain how to write an application to get 
  7.  * the second row of the sample table if the column oids are .1.3.6.1.4.1.2.2.1.1, 
  8.  * .1.3.6.1.4.1.2.2.1.2, .1.3.6.1.4.1.2.2.1.3 by using 
  9.  * com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
  10.  *
  11.  * The user could run this application by giving the following usage.
  12.  *  
  13.  * java GetRowData
  14.  *
  15.  *
  16. import java.lang.*;
  17. import java.util.*;
  18. import java.net.*;
  19. import com.adventnet.snmp.snmp2.*;
  20. public class GetRowData {
  21. public static void main(String args[]) {
  22.  // Start SNMP API
  23.         SnmpAPI api = new SnmpAPI();
  24. // Open session
  25. SnmpSession session = new SnmpSession(api); 
  26. try 
  27. {
  28. session.open();
  29. }
  30. catch (SnmpException e )
  31. {
  32. System.err.println("Error opening socket: "+e);
  33. }
  34. // Build Get request PDU
  35. SnmpPDU pdu = new SnmpPDU();
  36. UDPProtocolOptions option = new UDPProtocolOptions("localhost");
  37. pdu.setProtocolOptions(option); 
  38. // get the OIDs
  39. SnmpOID[] oids=new SnmpOID[3]; 
  40. oids[0]=new SnmpOID(".1.3.6.1.4.1.2.2.1.1.6"); 
  41. oids[1]=new SnmpOID(".1.3.6.1.4.1.2.2.1.2.6"); 
  42. oids[2]=new SnmpOID(".1.3.6.1.4.1.2.2.1.3.6"); 
  43. // Send PDU and receive response PDU
  44. for(int i=0;i<oids.length;i++) 
  45. {
  46. pdu.addNull(oids[i]); 
  47. }
  48. pdu.setCommand(SnmpAPI.GET_REQ_MSG); 
  49. session.open();
  50. SnmpPDU result = session.syncSend(pdu);
  51. if (result == null)
  52. {
  53. System.out.println("Request timed out!");
  54. }
  55. else
  56. {
  57. System.out.println(result.printVarBinds() + "n" );
  58. }
  59. // close session
  60. session.close();
  61. // close api thread
  62. api.close();
  63.  }
  64. }