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

SNMP编程

开发平台:

C/C++

  1. /*
  2.  * @(#)Snmpexample.java
  3.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  4.  * Please read the associated COPYRIGHTS file for more details.
  5.  */
  6. import java.lang.*;
  7. import java.util.*;
  8. import java.net.*;
  9. import com.adventnet.snmp.snmp2.*;
  10. public class snmpexample  {
  11. public static void main(String args[]) {
  12.         
  13. // Start SNMP API
  14.         SnmpAPI api = new SnmpAPI();
  15. api.setDebug(true);
  16. // Open session
  17. SnmpSession session = new SnmpSession(api); 
  18. try 
  19. {
  20.          session.open();
  21.         } 
  22. catch (SnmpException e ) 
  23. {
  24. System.err.println("Error opening socket: "+e);
  25. }
  26. // Build Get request PDU
  27. SnmpPDU pdu = new SnmpPDU();
  28. UDPProtocolOptions option = new UDPProtocolOptions("localhost");
  29. pdu.setProtocolOptions(option); //sets the host 
  30. pdu.setCommand(SnmpAPI.GET_REQ_MSG); 
  31. // add OID
  32. SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.1.0");
  33. pdu.addNull(oid);
  34. //send request
  35. try 
  36. {
  37. SnmpPDU result = session.syncSend(pdu);
  38. }
  39. catch (SnmpException e) 
  40. {
  41. System.err.println("Error sending SNMP request: "+e);
  42. }
  43. System.out.println(result.printVarBinds());
  44. // close session
  45. session.close();
  46. //close the api thread
  47. api.close();
  48.  }
  49. }