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

SNMP编程

开发平台:

C/C++

  1. * @(#)SnmpGetNext.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 
  7.  * application to retrieve information about the node 
  8.  * next to the sysName in the localhost's RFC1213-MIB 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 SnmpGetNext
  14.  *
  15.  *
  16. import java.lang.*;
  17. import java.util.*;
  18. import java.net.*;
  19. import com.adventnet.snmp.snmp2.*;
  20. public class SnmpGetNext {
  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 GetNext request PDU
  35. SnmpPDU pdu = new SnmpPDU();
  36. UDPProtocolOptions option = new UDPProtocolOptions("localhost");
  37. pdu.setProtocolOptions(option); 
  38. pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG );
  39. // Provide the OID .1.3.6.1.2.1.1.4
  40. SnmpOID oid = new SnmpOID("1.1.4");
  41. pdu.addNull(oid);
  42. // Send PDU and receive response PDU
  43. SnmpPDU result = session.syncSend(pdu);
  44. if (result == null)
  45. {
  46. System.out.println("Request timed out!");
  47. }
  48. else
  49. {
  50. if (result.getErrstat()== 0)
  51. {
  52. // print the response pdu varbinds
  53. System.out.println(result.printVarBinds());
  54. }
  55. else
  56. {
  57. System.out.println(result.getError());
  58. }
  59. }
  60.  // close session
  61. session.close();
  62. // close api thread
  63. api.close();
  64.  }
  65. }