使用snmp4j实现Snmp功能

上传人:大米 文档编号:564422125 上传时间:2023-12-13 格式:DOC 页数:7 大小:70.50KB
返回 下载 相关 举报
使用snmp4j实现Snmp功能_第1页
第1页 / 共7页
使用snmp4j实现Snmp功能_第2页
第2页 / 共7页
使用snmp4j实现Snmp功能_第3页
第3页 / 共7页
使用snmp4j实现Snmp功能_第4页
第4页 / 共7页
使用snmp4j实现Snmp功能_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《使用snmp4j实现Snmp功能》由会员分享,可在线阅读,更多相关《使用snmp4j实现Snmp功能(7页珍藏版)》请在金锄头文库上搜索。

1、使用实现功能一篇有关的文章已经是一年前写的了,因为工作等各种原因,一直没有继续下去。但是不管怎么样,包括,虽然速度有点慢,我还是会坚持学习并将心得写下去。),里的各种功能。首上一篇文章讲了的一些基本概念(详情请查看这里接下来,我们使用的开源组件来实现一下先是上一篇文章中的那个例子。即通过获取机器名。的包可以在它的官方网站上下载,我就不啰嗦了。接下来直接贴代码:importjava.io.IOException;importjava.util.Vector;importorg.snmp4j.CommunityTarget;importorg.snmp4j.PDU;importorg.snmp4j

2、.Snmp;importorg.snmp4j.TransportMapping;importorg.snmp4j.event.ResponseEvent;importorg.snmp4j.mp.SnmpConstants;importorg.snmp4j.smi.Address;importorg.snmp4j.smi.GenericAddress;importorg.snmp4j.smi.OID;importorg.snmp4j.smi.OctetString;importorg.snmp4j.smi.VariableBinding;importorg.snmp4j.transport.De

3、faultUdpTransportMapping;publicclassSnmpUtilprivateSnmpsnmp=null;privateAddresstargetAddress=null;publicvoidinitComm()throwsIOException/设置Agent方的IP和端口targetAddress=GenericAddress.parse(udp:127.0.0.1/161);TransportMappingtransport=newDefaultUdpTransportMapping();snmp=newSnmp(transport);transport.list

4、en();publicvoidsendPDU()throwsIOException/设置targetCommunityTargettarget=newCommunityTarget();target.setCommunity(newOctetString(public);target.setAddress(targetAddress);/通信不成功时的重试次数target.setRetries(2);/超时时间target.setTimeout(1500);target.setVersion(SnmpConstants.version1);/创建PDUPDUpdu=newPDU();pdu.a

5、dd(newVariableBinding(newOID(newint1,3,6,1,2,1,1,5,0);/MIB的访问方式pdu.setType(PDU.GET);/向Agent发送PDU,并接收ResponseResponseEventrespEvnt=snmp.send(pdu,target);/解析Responseif(respEvnt!=null&respEvnt.getResponse()!=null)VectorrecVBs=respEvnt.getResponse().getVariableBindings();for(inti=0;irecVBs.size();i+)Var

6、iableBindingrecVB=recVBs.elementAt(i);System.out.println(recVB.getOid()+:+recVB.getVariable();publicstaticvoidmain(Stringargs)trySnmpUtilutil=newSnmpUtil();util.initComm();util.sendPDU();catch(IOExceptione)e.printStackTrace();上面的这段代码直接参考nmp说明文档中提供的例子,是一个最简单的nmp的应用。只要你的机器里安装了nmp通讯组件,上面的代码应该可以运行成功。在上一

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

8、yTarget;importorg.snmp4j.PDU;importorg.snmp4j.Snmp;importorg.snmp4j.TransportMapping;importorg.snmp4j.event.ResponseEvent;importorg.snmp4j.mp.SnmpConstants;importorg.snmp4j.smi.Address;importorg.snmp4j.smi.GenericAddress;importorg.snmp4j.smi.OID;importorg.snmp4j.smi.OctetString;importorg.snmp4j.smi.

9、VariableBinding;importorg.snmp4j.transport.DefaultUdpTransportMapping;publicclassSnmpUtilprivateSnmpsnmp=null;privateAddresstargetAddress=null;publicvoidinitComm()throwsIOException/设置Agent方的IP和端口targetAddress=GenericAddress.parse(udp:127.0.0.1/161);TransportMappingtransport=newDefaultUdpTransportMap

10、ping();snmp=newSnmp(transport);transport.listen();publicResponseEventsendPDU(PDUpdu)throwsIOException/设置targetCommunityTargettarget=newCommunityTarget();target.setCommunity(newOctetString(public);target.setAddress(targetAddress);/通信不成功时的重试次数target.setRetries(2);/超时时间target.setTimeout(1500);target.se

11、tVersion(SnmpConstants.version1);/向Agent发送PDU,并返回Responsereturnsnmp.send(pdu,target);publicvoidsetPDU()throwsIOException/setPDUPDUpdu=newPDU();pdu.add(newVariableBinding(newOID(newint1,3,6,1,2,1,1,5,0),newOctetString(SNMPTEST);pdu.setType(PDU.SET);sendPDU(pdu);publicvoidgetPDU()throwsIOException/get

12、PDUPDUpdu=newPDU();pdu.add(newVariableBinding(newOID(newint1,3,6,1,2,1,1,5,0);pdu.setType(PDU.GET);readResponse(sendPDU(pdu);publicvoidreadResponse(ResponseEventrespEvnt)/解析Responseif(respEvnt!=null&respEvnt.getResponse()!=null)VectorrecVBs=respEvnt.getResponse().getVariableBindings();for(inti=0;ire

13、cVBs.size();i+)VariableBindingrecVB=recVBs.elementAt(i);System.out.println(recVB.getOid()+:+recVB.getVariable();publicstaticvoidmain(Stringargs)trySnmpUtilutil=newSnmpUtil();util.initComm();util.setPDU();util.getPDU();catch(IOExceptione)e.printStackTrace();如果控制台打出“1.3.6.1.2.1.1.5.0:SNMPTEST”的消息,就说明我

14、们的操作成功啦!前一篇文章讲了如何用实现和的功能,今天讲如何接收。提供了一个抽象类类用于接收,这个类里面有一个必须实现的方法,当接收到,会自动进入这个方法,因此我们可以将对的处理写在这里。下面修改上篇文章例子中的方法:privateTransportMappingtransport=null;publicvoidinitComm()throwsIOException/设置Agent方的IP和端口targetAddress=GenericAddress.parse(udp:192.168.1.1/161);/设置接收trap的IP和端口transport=newDefaultUdpTransportMapping(newUdpAddress(192.168.1.2/162);snmp=newSnmp(transport);CommandRespondertrapRec=newCommandResponder()publicsynchronizedvoidprocessPdu(CommandResponderEvente)/接收trapPDUcommand=e.getPDU();if(command!=null)System.out.println(command.toString();;snmp.addCommandResponder(trapRec);transport

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号