使用snmp4j实现Snmp功能(一)

 

插播一段声明:楼主我其实就用了小半年的Snmp而已,就把学习心得发了出来,目的就是能够引导大家入门,但是更深入的知识我就不知道啦。请大家不要再问我问题了,我知道的都已经写出来啦,谢谢!

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

上一篇文章讲了Snmp的一些基本概念(Snmp学习笔记),接下来,我们使用Java的开源组件snmp4j来实现一下Snmp里的各种功能。首先是上一篇文章中的那个例子。即通过snmp获取机器名。

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

接下来直接贴代码:

import java.io.IOException;

import java.util.Vector;

import org.snmp4j.CommunityTarget;

import org.snmp4j.PDU;

import org.snmp4j.Snmp;

import org.snmp4j.TransportMapping;

import org.snmp4j.event.ResponseEvent;

import org.snmp4j.mp.SnmpConstants;

import org.snmp4j.smi.Address;

import org.snmp4j.smi.GenericAddress;

import org.snmp4j.smi.OID;

import org.snmp4j.smi.OctetString;

import org.snmp4j.smi.VariableBinding;

import org.snmp4j.transport.DefaultUdpTransportMapping;

publicclass SnmpUtil {

private Snmp snmp = null;

private Address targetAddress = null;

publicvoid initComm() throws IOException {

// Agent方的IP和端口

targetAddress = GenericAddress.parse("udp:127.0.0.1/161");

TransportMapping transport = new DefaultUdpTransportMapping();

snmp = new Snmp(transport);

transport.listen();

}

publicvoid sendPDU() throws IOException {

// target

CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString("public"));

target.setAddress(targetAddress);

// 通信不成功的重次数

target.setRetries(2);

// 时时间

target.setTimeout(1500);

target.setVersion(SnmpConstants.version1);

// PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));

// MIB访问方式

pdu.setType(PDU.GET);

// AgentPDU,并接收Response

ResponseEvent respEvnt = snmp.send(pdu, target);

// 解析Response

if (respEvnt != null && respEvnt.getResponse() != null) {

Vector<VariableBinding> recVBs = respEvnt.getResponse()

.getVariableBindings();

for (int i = 0; i < recVBs.size(); i++) {

VariableBinding recVB = recVBs.elementAt(i);

System.out.println(recVB.getOid() + " : " + recVB.getVariable());

}

}

}

publicstaticvoid main(String[] args) {

try {

SnmpUtil util = new SnmpUtil();

util.initComm();

util.sendPDU();

} catch (IOException e) {

e.printStackTrace();

}

}

}


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

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

import java.io.IOException;

import java.util.Vector;

import org.snmp4j.CommunityTarget;

import org.snmp4j.PDU;

import org.snmp4j.Snmp;

import org.snmp4j.TransportMapping;

import org.snmp4j.event.ResponseEvent;

import org.snmp4j.mp.SnmpConstants;

import org.snmp4j.smi.Address;

import org.snmp4j.smi.GenericAddress;

import org.snmp4j.smi.OID;

import org.snmp4j.smi.OctetString;

import org.snmp4j.smi.VariableBinding;

import org.snmp4j.transport.DefaultUdpTransportMapping;

publicclass SnmpUtil {

private Snmp snmp = null;

private Address targetAddress = null;

publicvoid initComm() throws IOException {

// Agent方的IP和端口

targetAddress = GenericAddress.parse("udp:127.0.0.1/161");

TransportMapping transport = new DefaultUdpTransportMapping();

snmp = new Snmp(transport);

transport.listen();

}

public ResponseEvent sendPDU(PDU pdu) throws IOException {

// target

CommunityTarget target = new CommunityTarget();

target.setCommunity(new OctetString("public"));

target.setAddress(targetAddress);

// 通信不成功的重次数

target.setRetries(2);

// 时时间

target.setTimeout(1500);

target.setVersion(SnmpConstants.version1);

// AgentPDU,并返回Response

returnsnmp.send(pdu, target);

}

publicvoid setPDU() throws IOException {

// set PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 }), new OctetString("SNMPTEST")));

pdu.setType(PDU.SET);

sendPDU(pdu);

}

publicvoid getPDU() throws IOException {

// get PDU

PDU pdu = new PDU();

pdu.add(new VariableBinding(new OID(newint[] { 1, 3, 6, 1, 2, 1, 1, 5, 0 })));

pdu.setType(PDU.GET);

readResponse(sendPDU(pdu));

}

publicvoid readResponse(ResponseEvent respEvnt) {

// 解析Response

if (respEvnt != null && respEvnt.getResponse() != null) {

Vector<VariableBinding> recVBs = respEvnt.getResponse()

.getVariableBindings();

for (int i = 0; i < recVBs.size(); i++) {

VariableBinding recVB = recVBs.elementAt(i);

System.out.println(recVB.getOid() + " : " + recVB.getVariable());

}

}

}

publicstaticvoid main(String[] args) {

try {

SnmpUtil util = new SnmpUtil();

util.initComm();

util.setPDU();

util.getPDU();

} catch (IOException e) {

e.printStackTrace();

}

}

}

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值