分享

使用snmp4j实现Snmp功能(一)

 kekokeko 2010-11-25

使用snmp4j实现Snmp功能(一)

上一篇有关Snmp的文章已经是一年前写的了,因为工作等各种原因,一直没有继续下去。但是不管怎么样,包括AppFuse,虽然速度有点慢,我还是会坚持学习并将心得写下去。

上一篇文章讲了Snmp的一些基本概念(详情请查看这里http://blog.csdn.net/clearwater21cn/archive/2007/06/26/1667614.aspx),接下来,我们使用Java的开源组件snmp4j来实现一下Snmp里的各种功能。首先是上一篇文章中的那个例子。即通过snmp获取机器名。

snmp4j的jar包可以在它的官方网站http://www./上下载,我就不啰嗦了。

Java代码 复制代码
  1. import java.io.IOException;   
  2.   
  3. import java.util.Vector;   
  4.   
  5.   
  6. import org.snmp4j.CommunityTarget;   
  7.   
  8. import org.snmp4j.PDU;   
  9.   
  10. import org.snmp4j.Snmp;   
  11.   
  12. import org.snmp4j.TransportMapping;   
  13.   
  14. import org.snmp4j.event.ResponseEvent;   
  15.   
  16. import org.snmp4j.mp.SnmpConstants;   
  17.   
  18. import org.snmp4j.smi.Address;   
  19.   
  20. import org.snmp4j.smi.GenericAddress;   
  21.   
  22. import org.snmp4j.smi.OID;   
  23.   
  24. import org.snmp4j.smi.OctetString;   
  25.   
  26. import org.snmp4j.smi.VariableBinding;   
  27.   
  28. import org.snmp4j.transport.DefaultUdpTransportMapping;   
  29.   
  30.   
  31. public class SnmpUtil {   
  32.   
  33.   
  34.        private Snmp snmp = null;   
  35.   
  36.   
  37.        private Address targetAddress = null;   
  38.   
  39.   
  40.        public void initComm() throws IOException {   
  41.   
  42.                  
  43.   
  44.               // 设置Agent方的IP和端口   
  45.   
  46.               targetAddress = GenericAddress.parse("udp:127.0.0.1/161");   
  47.   
  48.               TransportMapping transport = new DefaultUdpTransportMapping();   
  49.   
  50.               snmp = new Snmp(transport);   
  51.   
  52.               transport.listen();   
  53.   
  54.        }   
  55.   
  56.   
  57.        public void sendPDU() throws IOException {   
  58.   
  59.               // 设置 target   
  60.   
  61.               CommunityTarget target = new CommunityTarget();   
  62.   
  63.               target.setCommunity(new OctetString("public"));   
  64.   
  65.               target.setAddress(targetAddress);   
  66.   
  67.               // 通信不成功时的重试次数   
  68.   
  69.               target.setRetries(2);   
  70.   
  71.               // 超时时间   
  72.   
  73.               target.setTimeout(1500);   
  74.   
  75.               target.setVersion(SnmpConstants.version1);   
  76.   
  77.               // 创建 PDU   
  78.   
  79.               PDU pdu = new PDU();   
  80.   
  81.               pdu.add(new VariableBinding(new OID(new int[] { 136121150 })));   
  82.   
  83.               // MIB的访问方式   
  84.   
  85.               pdu.setType(PDU.GET);   
  86.   
  87.               // 向Agent发送PDU,并接收Response   
  88.   
  89.               ResponseEvent respEvnt = snmp.send(pdu, target);   
  90.   
  91.               // 解析Response   
  92.   
  93.               if (respEvnt != null && respEvnt.getResponse() != null) {   
  94.   
  95.                      Vector<VariableBinding> recVBs = respEvnt.getResponse()   
  96.   
  97.                                    .getVariableBindings();   
  98.   
  99.                      for (int i = 0; i < recVBs.size(); i++) {   
  100.   
  101.                             VariableBinding recVB = recVBs.elementAt(i);   
  102.   
  103.                             System.out.println(recVB.getOid() + " : " + recVB.getVariable());   
  104.   
  105.                      }   
  106.   
  107.               }   
  108.   
  109.        }   
  110.   
  111.   
  112.        public static void main(String[] args) {   
  113.   
  114.               try {   
  115.   
  116.                      SnmpUtil util = new SnmpUtil();   
  117.   
  118.                      util.initComm();   
  119.   
  120.                      util.sendPDU();   
  121.   
  122.               } catch (IOException e) {   
  123.   
  124.                      e.printStackTrace();   
  125.   
  126.               }   
  127.   
  128.        }   
  129.   
  130. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多