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

SNMP编程

开发平台:

C/C++

  1. * @(#)SnmpSet.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 set the value of sysName to "localhost" using 
  8.  * com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
  9.  *
  10.  * The user could run this application by giving the following usage.
  11.  *  
  12.  * java SnmpSet
  13.  *
  14.  *
  15. import java.lang.*;
  16. import java.util.*;
  17. import java.net.*;
  18. import com.adventnet.snmp.snmp2.*;
  19. public class SnmpSet {
  20. public static void main(String args[]) {
  21.  // Start SNMP API
  22.         SnmpAPI api = new SnmpAPI();
  23. // Open session
  24. SnmpSession session = new SnmpSession(api); 
  25. try 
  26. {
  27. session.open();
  28. }
  29. catch (SnmpException e )
  30. {
  31. System.err.println("Error opening socket: "+e);
  32. }
  33. // Build set request PDU
  34. SnmpPDU pdu = new SnmpPDU();
  35. pdu.setCommand(SnmpAPI.SET_REQ_MSG );
  36. // Provide the OID .1.3.6.1.2.1.5.0
  37. SnmpOID oid = new SnmpOID("1.5.0"); 
  38. // provide the value "localhost"
  39. String value = "localhost";
  40. // provide the type "STRING"
  41. byte dataType = SnmpAPI.STRING;
  42. SnmpVar var = null;
  43. try
  44. {
  45. // create SnmpVar instance for the value and the type
  46. var = SnmpVar.createVariable(value, dataType);
  47. }
  48. catch(SnmpException e)
  49. {
  50. System.err.println("Cannot create variable " + oid + " with value " + value);
  51. return;
  52. }
  53. //create varbind
  54. SnmpVarBind varbind = new SnmpVarBind(oid, var);
  55. //add variable binding 
  56. pdu.addVariableBinding(varbind);
  57. // Send PDU
  58. SnmpPDU result = session.syncSend(pdu);
  59. if (result == null)
  60. {
  61. System.out.println("Request timed out!");
  62. }
  63. else
  64. {
  65. if (result.getErrstat()== 0)
  66. {
  67. // print the response pdu varbinds
  68. System.out.println(result.printVarBinds());
  69. }
  70. else
  71. {
  72. System.out.println(result.getError());
  73. }
  74. }
  75.  // close session
  76. session.close();
  77. // close api thread
  78. api.close();
  79.  }
  80. }