使用snmp4j实现Snmp功能(一)

来源:互联网 发布:松本行弘 知乎 编辑:程序博客网 时间:2024/05/22 11:41

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

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

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

 

[java] view plain copy
  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. }  

上面的这段代码直接参考snmp4j API说明文档中提供的例子,是一个最简单的snmp4j的应用。只要你的机器里安装了snmp通讯组件,上面的代码应该可以运行成功。

在上一个例子中,我们只做了读取的工作,接下来,我们进行一下设置操作,通过Snmp修改读取的机器名。
    public的默认权限是只读,要想进行写操作,我们必须进行手动的设置。具体的做法是:进入管理工具→服务,找到Snmp Service→属性→安全。在这个选项卡中我们可以看到public的权限是只读,你可以修改public的权限,也可以重新创建一个community。从安全角度来讲当然应该新建一个,在这里为了测试方便,我就直接给public添加写入权限了。
    接下来就可以编写代码了,我把上面的例子重构一下,代码如下:

 

[java] view plain copy
  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 ResponseEvent sendPDU(PDU pdu) 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.               // 向Agent发送PDU,并返回Response  
  78.   
  79.               return snmp.send(pdu, target);  
  80.   
  81.        }  
  82.   
  83.          
  84.   
  85.        public void setPDU() throws IOException {  
  86.   
  87.               // set PDU  
  88.   
  89.               PDU pdu = new PDU();  
  90.   
  91.               pdu.add(new VariableBinding(new OID(new int[] { 136121150 }), new OctetString("SNMPTEST")));  
  92.   
  93.               pdu.setType(PDU.SET);  
  94.   
  95.               sendPDU(pdu);  
  96.   
  97.        }  
  98.   
  99.          
  100.   
  101.        public void getPDU() throws IOException {  
  102.   
  103.               // get PDU  
  104.   
  105.               PDU pdu = new PDU();  
  106.   
  107.               pdu.add(new VariableBinding(new OID(new int[] { 136121150 })));  
  108.   
  109.               pdu.setType(PDU.GET);  
  110.   
  111.               readResponse(sendPDU(pdu));  
  112.   
  113.        }  
  114.   
  115.          
  116.   
  117.        public void readResponse(ResponseEvent respEvnt) {  
  118.   
  119.               // 解析Response  
  120.   
  121.               if (respEvnt != null && respEvnt.getResponse() != null) {  
  122.   
  123.                      Vector<VariableBinding> recVBs = respEvnt.getResponse()  
  124.   
  125.                                    .getVariableBindings();  
  126.   
  127.                      for (int i = 0; i < recVBs.size(); i++) {  
  128.   
  129.                             VariableBinding recVB = recVBs.elementAt(i);  
  130.   
  131.                             System.out.println(recVB.getOid() + " : " + recVB.getVariable());  
  132.   
  133.                      }  
  134.   
  135.               }  
  136.   
  137.        }  
  138.   
  139.          
  140.   
  141.        public static void main(String[] args) {  
  142.   
  143.               try {  
  144.   
  145.                      SnmpUtil util = new SnmpUtil();  
  146.   
  147.                      util.initComm();  
  148.   
  149.                      util.setPDU();  
  150.   
  151.                      util.getPDU();  
  152.   
  153.               } catch (IOException e) {  
  154.   
  155.                      e.printStackTrace();  
  156.   
  157.               }  
  158.   
  159.        }  
  160.   
  161. }  

如果控制台打出“1.3.6.1.2.1.1.5.0 : SNMPTEST”的消息,就说明我们的操作成功啦!

以上代码在WindowsXP下测试成功。

0 0
原创粉丝点击