SNMP4j.java
上传用户:weishen1
上传日期:2021-09-15
资源大小:1k
文件大小:3k
源码类别:

SNMP编程

开发平台:

Java

  1. package com.ultrapower.cruiser.snmp;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Hashtable;
  5. import java.util.List;
  6. import javax.management.ObjectName;
  7. import org.snmp4j.CommunityTarget;
  8. import org.snmp4j.PDU;
  9. import org.snmp4j.Snmp;
  10. import org.snmp4j.event.ResponseEvent;
  11. import org.snmp4j.mp.SnmpConstants;
  12. import org.snmp4j.smi.OID;
  13. import org.snmp4j.smi.OctetString;  
  14. import org.snmp4j.smi.UdpAddress;
  15. import org.snmp4j.smi.VariableBinding;
  16. import org.snmp4j.transport.DefaultUdpTransportMapping;
  17. public class SNMP4j{
  18. public static String connectSnmpServer(int port, String hostname,
  19. String community,String oid_) {
  20. try {
  21. Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
  22. CommunityTarget target = new CommunityTarget();// agent对象
  23. target.setCommunity(new OctetString(community));// 设置共同体名,没发现设置RWCommnity的方法,大概只能设一个.
  24. target.setVersion(SnmpConstants.version1);// 设置版本
  25. target.setAddress(new UdpAddress(hostname+"/"+port));// 设置IP地址和端口号,这里竟然用'/'来分隔,当初确实没有料到,JDOC的说明等于没有.
  26. target.setRetries(1); // 设置重试次数
  27. target.setTimeout(5000); // 设置超时
  28. snmp.listen(); // 监听
  29. String m_oid =oid_;
  30. // set pud type and set oid
  31. PDU request = new PDU(); // new request PDU包
  32. request.setType(PDU.GETNEXT); // 设置PDU类型,
  33. request.add(new VariableBinding(new OID(m_oid)));
  34. PDU response = null;// 定义response包
  35. ResponseEvent responseEvent = snmp.send(request, target); // 发出request
  36. // PDU
  37. // 接收response PDU
  38. response = responseEvent.getResponse();
  39. // response PDU包解析
  40. if (response != null) {
  41. System.out.println("response : " + response.toString());
  42. if (response.getErrorIndex() == response.noError
  43. && response.getErrorStatus() == response.noError) {
  44. String pause = responseEvent.getResponse()
  45. .getVariableBindings().toString();
  46. String getvalue = pause.substring(
  47. pause.indexOf("= ") + 2, pause.indexOf(']'));
  48. String oid = pause.substring(pause.indexOf("VBS[") + 2,
  49. pause.indexOf("=") - 1);
  50. System.out.println("oid : " + oid);
  51. System.out.println(getvalue);
  52. return getvalue;
  53. } else {
  54. System.out.println("get error:"
  55. + response.getErrorStatusText());
  56. }
  57. } else {
  58. System.out.println("get response error");
  59. }
  60. snmp.close();
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. return null;
  66. }
  67. public static void  main(String s[]){
  68. SNMP4j snmp = new SNMP4j();
  69. String ss = snmp.connectSnmpServer(161,"192.168.5.122","public",".1.3.6.1.4.1.140.625.10.1");
  70. // String ss = snmp.connectSnmpServer(161,"192.168.0.181","public",".1.3.6.1.4.1.9.2.1.59");
  71. System.out.println("the ss is ; "+ss);
  72. SnmpTool smt = new SnmpTool();
  73. smt.setPduType(PDU.GETNEXT);
  74. System.out.println(smt.getMibValue(161,"public","192.168.5.122",".1.3.6.1.4.1.140.625.105.1.5"));
  75. }
  76. }