基本功能的测试,Snmp类的封装有些怪异,不看源码会写错。
import java.io.IOException;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SnmpTest {
private final static Logger LOG = LoggerFactory.getLogger(SnmpTest.class);
private static Snmp snmp;
private static CommunityTarget target1;
private static CommunityTarget target2;
@BeforeClass
public static void init() throws IOException {
// SNMP echo agent at port 50162
// snmpListen = new Snmp(new DefaultUdpTransportMapping(new
// UdpAddress(50162), true));
snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommandResponder listener = new CommandResponder() {
@Override
public void processPdu(CommandResponderEvent event) {
LOG.info(event.getTransportMapping().getListenAddress() + " Get request: {}", event.getPDU());
PDU pdu = new PDU(event.getPDU()); // echo
pdu.setType(PDU.RESPONSE);
pdu.setRequestID(event.getPDU().getRequestID());
CommunityTarget respTarget = new CommunityTarget(event.getPeerAddress(), new OctetString("public"));
// respTarget.setRetries(2);
// respTarget.setTimeout(1500);
// respTarget.setVersion(SnmpConstants.version2c);
try { // send an empty response PDU
snmp.send(pdu, respTarget);
} catch (IOException e) {
LOG.error(null, e);
}
}
};
snmp.addNotificationListener(new UdpAddress(50162), listener);
snmp.addNotificationListener(new UdpAddress(50168), listener);
// session to send
target1 = new CommunityTarget(UdpAddress.parse("localhost/50162"), new OctetString("public"));
// target.setRetries(2);
// target.setTimeout(1500);
target1.setVersion(SnmpConstants.version2c); // support getBulk
target2 = new CommunityTarget(UdpAddress.parse("localhost/50168"), new OctetString("public"));
}
@AfterClass
public static void destroy() throws IOException {
snmp.close();
}
@Test
public void testSnmpSend() throws InterruptedException, IOException {
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
checkResponse(snmp.get(pdu, target1));
checkResponse(snmp.getNext(pdu, target1));
checkResponse(snmp.getBulk(pdu, target1));
checkResponse(snmp.set(pdu, target1));
checkResponse(snmp.inform(pdu, target1));
pdu.setType(PDU.TRAP);
snmp.send(pdu, target1);
}
@Test
public void testSnmpSendAsync() throws Exception {
// creating PDU
final PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 2 })));
pdu.setType(PDU.GETNEXT);
// sending request
final ResponseListener listener = new ResponseListener() {
public void onResponse(ResponseEvent event) {
// important!!!
((Snmp) event.getSource()).cancel(event.getRequest(), this);
checkResponse(event);
synchronized (pdu) {
pdu.notifyAll();
}
}
};
snmp.getNext(pdu, target2, null, listener);
synchronized (pdu) {
pdu.wait(5000);
}
}
private void checkResponse(ResponseEvent event) {
if (event == null) {
Assert.fail("response timeout");
} else if (event.getError() != null) {
LOG.error("Get response error", event.getError());
Assert.fail("failed: " + event.getError().getLocalizedMessage());
} else if (event.getResponse() == null) {
Assert.fail("response timeout");
} else
LOG.info("Get response {}: {}", Thread.currentThread(), event.getResponse());
}
@Test
public void testTable() {
// TableUtils util = new TableUtils(snmp,new DefaultPDUFactory());
// OID oidIndex;
// OID oidStatus;
// VariableBinding[] values;
// util.createRow(target1, oidStatus, oidIndex, values);
// util.destroyRow(target1, oidIndex, oidIndex);
// OID[] columnOIDs;
// util.getTable(target1, columnOIDs, null, oidIndex);
}
}
分享到:
相关推荐
在这个压缩包中,您将找到与配置SNMP v3相关的所有必要文件,包括安装程序、测试代码以及SNMP4J的Java库。 首先,让我们详细了解SNMP v3的关键特性。相比之前的SNMP v1和v2c,SNMP v3引入了用户安全模型(USM),...
将这个文件添加到项目的类路径中,就可以在Java代码中直接调用SNMP4J的功能,进行SNMP通信。 使用SNMP4J时,开发者通常会遵循以下步骤: 1. **创建TransportMapping对象**:这一步定义了SNMP消息的传输方式,如UDP...
本文将深入探讨如何使用SNMP4J-Agent工具在个人计算机上模拟SNMP服务,以便进行测试和开发。 SNMP4J-Agent是SNMP4J项目的一部分,是一个开源的Java库,用于实现SNMP代理功能。它提供了丰富的API,可以方便地构建...
总的来说,这个压缩包提供了一个了解和实践SNMP4J的完整环境,从基本的示例代码到运行环境的搭建。通过学习这个入门实例,开发者可以掌握如何使用SNMP4J进行设备查询和配置,同时对SNMP协议有更深入的理解。而XP ...
开发者可以参考这些测试来编写自己的测试代码,确保应用与SNMP4J的兼容性。 使用SNMP4J 2.0.1,开发者可以轻松实现以下功能: 1. **网络设备的监控**:通过SNMP查询网络设备的性能指标,如CPU利用率、内存使用情况...
**SNMP4J 1.10版本开发包详解** SNMP4J是Apache公司提供的一款开源Java库,专门用于实现简单网络管理协议(SNMP)的应用开发。这个开发包包含`.jar`库文件以及源代码,使得开发者能够轻松地在Java项目中集成SNMP...
在这个"snmp4j-2.5.11"压缩包中,用户可以找到SNMP4J库的源代码和对应的jar包。源码对于开发者来说是非常宝贵的,因为它允许他们查看并理解库内部的工作机制,定制功能,或者调试可能出现的问题。jar包则是编译后的...
通过MIB(管理信息库)代码生成,SNMP4J-Agent可以帮助开发者快速地将MIB文件转换为Java代码,简化开发过程。 总的来说,SNMP4J是Java环境中开发SNMP应用的强大工具,无论是对于网络设备监控,还是构建自定义的网络...
SNMP4J是一个Java实现的简单网络管理协议(SNMP)的应用编程接口(API),它为开发者提供了在Java环境中开发SNMP应用的功能。这个库主要适用于网络设备的管理和监控,例如路由器、交换机和其他网络硬件。SNMP4J ...
10. **工具集**:SNMP4j_tool 可能是随库附带的一些实用工具,如SNMP代理模拟器、命令行查询工具等,帮助开发者测试和调试SNMP功能。 总的来说,SNMP4j.jar 是Java开发者在实现网络设备管理、监控和自动化任务时不...
在这个压缩包“snmp4j-1.11.1.zip”中,我们很可能会找到包括SNMP4J的核心库文件、相关的文档、示例代码和可能的测试资源。 SNMP是一种广泛用于网络设备管理的标准协议,它允许管理员远程监控和管理网络设备,如...
SNMP4J是一个开源的、全面的SNMP开发工具包,提供了对SNMPv1、v2c和v3的支持。 4. **创建SNMP代理**: - 首先,你需要在项目中导入SNMP4J库。在Eclipse中,可以通过右键点击项目 -> Build Path -> Configure Build...
但是,如果你在Visual C++环境下工作,可能需要使用JNI(Java Native Interface)来创建桥接,使得C++代码能调用SNMP4J的功能。 6. **C++封装**:在Visual C++中使用SNMP4J可能涉及到创建一个Java层和C++层之间的...
除了SNMP4J,压缩包文件中的`minitest.zip`提到的`minitest`是一个轻量级的测试库,适用于Scala和Scala.js项目。minitest为开发者提供了简洁、高效的测试框架,用于编写单元测试和集成测试。然而,由于主题的限制,...
3. `SNMP4JTestAgentBC.cfg`:SNMP4J是Java实现SNMP协议的库,这个文件可能是SNMP4J测试代理的配置文件。 4. `.classpath`:Eclipse或类似的IDE的配置文件,定义了项目的类路径,包含所有必要的库和依赖项。 5. `hs_...
以下是一个基本的Java代码示例,展示了如何使用Snmp4j来获取交换机的MAC地址: ```java import org.snmp4j.*; import org.snmp4j.mp.MPv1; import org.snmp4j.mp.MPv2c; import org.snmp4j.security.AuthMD5; ...
4. **文档**:尽管缺失,但完整的项目应提供详细的文档,解释如何编译、运行和测试代码。 为了进一步学习和使用"simple-snmp-nms",开发者需要: 1. **熟悉SNMP协议**:理解SNMP的基本概念、操作类型和安全模型。 2...
开发者需要通过SNMP库(如Python的pysnmp或Java的SNMP4J)来解析和读取这些信息。这个过程包括解析MIB文件,映射对象标识符(OID)到具体的变量,以及查询和获取这些变量的当前值。 2. **设备数据的修改**:SNMP...
源码包提供了`snmp4j`的完整代码,开发者可以通过阅读源码了解其内部实现,如如何处理SNMP报文的编码和解码,以及如何实现不同版本的SNMP协议。这对于学习SNMP协议和扩展自定义功能非常有帮助。 ### Windows测试...
这份名为"snmp-java-source_1.4.2"的压缩包提供了基于`snmp4j`的源代码,对于开发者来说是学习和应用SNMP的好资源。 **SNMP4J**是Java平台上的一个开源SNMP API,它提供了一套完整的类和接口,用于构建SNMP代理或...