- 浏览: 917927 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (263)
- J2EE (9)
- Spring (11)
- Hibernate (11)
- Struts (5)
- opensource (19)
- Hadoop (28)
- 架构设计 (8)
- 企业应用 (10)
- SNMP (8)
- SSO (4)
- webservice (11)
- RPC (2)
- 编程语言 (0)
- Java (30)
- Javascript (5)
- NoSQL (11)
- 数据库 (0)
- oracle (8)
- MySQL (3)
- web (1)
- Android (2)
- Linux (15)
- 软件工具 (15)
- 项目构建 (11)
- 测试工具 (2)
- Exception (19)
- 杂谈 (4)
- Groovy (5)
- Nodejs (1)
- MacOSX (4)
最新评论
-
fighhin:
decode(BinaryBitmap,java.util.M ...
条形码/二维码之开源利器ZXing图文介绍 -
u013489005:
追问:楼主,请问有中文文档么?我的邮箱是frankgray@s ...
Java表达式计算引擎:Expr4J -
u013489005:
感谢博主 需要引入的包是import java.io.*;im ...
Java表达式计算引擎:Expr4J -
calosteward:
感谢楼主分享。。 Zxing 我听说过的。__________ ...
条形码/二维码之开源利器ZXing图文介绍 -
u013810758:
judasqiqi 写道感谢楼主!想请问楼主一下这个生成的图片 ...
Java实现二维码QRCode的编码和解码
blog迁移至:http://www.micmiu.com
依赖于第三方SNMP4j来实现snmp的get、walk功能,主要实现了如下功能:
[一]、 snmp get 获取单个OID的值
SnmpGet.java
[二]、snmp get 同步和异步的方式获取多个OID的值
同步实现方法:SnmpGetList.java
异步实现方法:SnmpGetListAsyn.java
[三]、 SnmpWalk.java
[四]、SnmpUtil.java
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
-----------------------------------分 ------------------------------------隔 ------------------------------------线 --------------------------------------
这个util类里有很多其他的东西,这里的示例中只引用了一个方法,创建共同体SnmpUtil.createCommunityTarget(....),当时懒了点 没有贴的,已把SnmpUtil的代码清理了下,贴出了部分
依赖于第三方SNMP4j来实现snmp的get、walk功能,主要实现了如下功能:
- 一、snmp get 获取单个OID的值
- 二、snmp get 同步和异步的方式获取多个OID的值
- 三、snmp walk的方式
- 四、补充SnmpUtil.java代码
[一]、 snmp get 获取单个OID的值
SnmpGet.java
package com.michael.snmp4j; import java.io.IOException; import java.util.Vector; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import com.michael.snmp4j.util.SnmpUtil; /** * @see http://sjsky.iteye.com * @author Michael */ public class SnmpGet { private static int version = SnmpConstants.version1; private static String protocol = "udp"; private static int port = 161; /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; String oidval = "1.3.6.1.2.1.1.3.0"; SnmpGet tester = new SnmpGet(); tester.snmpGet(ip, community, oidval); } @SuppressWarnings("unchecked") private void snmpGet(String ip, String community, String oid) { String address = protocol + ":" + ip + "/" + port; CommunityTarget target = SnmpUtil.createCommunityTarget(address, community, version, 2 * 1000L, 3); DefaultUdpTransportMapping udpTransportMapping = null; Snmp snmp = null; try { PDU pdu = new PDU(); // pdu.add(new VariableBinding(new OID(new int[] // {1,3,6,1,2,1,1,2}))); pdu.add(new VariableBinding(new OID(oid))); pdu.setType(PDU.GET); udpTransportMapping = new DefaultUdpTransportMapping(); udpTransportMapping.listen(); snmp = new Snmp(udpTransportMapping); // 发送同步消息 ResponseEvent response = snmp.send(pdu, target); System.out.println("PeerAddress:" + response.getPeerAddress()); PDU responsePdu = response.getResponse(); if (responsePdu == null) { System.out.println(ip + ":Request time out"); } else { Vector vbVect = responsePdu.getVariableBindings(); System.out.println("vb size:" + vbVect.size()); if (vbVect.size() == 0) { System.out.println(" pdu vb size is 0 "); } else { Object obj = vbVect.firstElement(); VariableBinding vb = (VariableBinding) obj; System.out.println(vb.getOid() + " = " + vb.getVariable()); } } System.out.println("success finish snmp get the oid!"); } catch (Exception e) { System.out.println("SNMP Get Exception:" + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } if (udpTransportMapping != null) { try { udpTransportMapping.close(); } catch (IOException ex2) { udpTransportMapping = null; } } } } }
[二]、snmp get 同步和异步的方式获取多个OID的值
同步实现方法:SnmpGetList.java
package com.michael.snmp4j; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import com.michael.snmp4j.util.SnmpUtil; /** * @see http://sjsky.iteye.com * @author Michael * */ public class SnmpGetList { private static int version = SnmpConstants.version1; private static String protocol = "udp"; private static int port = 161; /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; SnmpGetList tester = new SnmpGetList(); List<String> oidList = new ArrayList<String>(); oidList.add(".1.3.6.1.2.1.1.1.0"); oidList.add(".1.3.6.1.2.1.1.3.0"); oidList.add(".1.3.6.1.2.1.1.5.0"); // synchronous tester.snmpGet(ip, community, oidList); } /** * * @param ipAddress * @param community * @param oid */ private void snmpGet(String ipAddress, String community, List<String> oidList) { String address = protocol + ":" + ipAddress + "/" + port; CommunityTarget target = SnmpUtil.createCommunityTarget(address, community, version, 2 * 1000L, 3); DefaultUdpTransportMapping transport = null; Snmp snmp = null; try { PDU pdu = new PDU(); pdu.setType(PDU.GET); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } transport = new DefaultUdpTransportMapping(); transport.listen(); snmp = new Snmp(transport); ResponseEvent response = snmp.send(pdu, target); PDU resPdu = response.getResponse(); if (resPdu == null) { System.out.println(ipAddress + ":Request time out"); } else { System.out.println(" response pdu vb size is " + resPdu.size()); for (int i = 0; i < resPdu.size(); i++) { VariableBinding vb = resPdu.get(i); System.out.println(vb.getOid() + "=" + vb.getVariable()); } } } catch (Exception e) { System.out.println("SNMP GetNext Exception:" + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } if (transport != null) { try { transport.close(); } catch (IOException ex2) { transport = null; } } } } }
异步实现方法:SnmpGetListAsyn.java
package michael.snmp.snmp4j; import java.util.ArrayList; import java.util.List; import michael.snmp.util.SnmpUtil; import org.snmp4j.CommunityTarget; import org.snmp4j.MessageDispatcherImpl; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; import org.snmp4j.mp.MPv1; import org.snmp4j.mp.MPv2c; import org.snmp4j.mp.MPv3; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.util.MultiThreadedMessageDispatcher; import org.snmp4j.util.ThreadPool; import org.snmp4j.util.WorkerPool; /** * asynchronous send PDU * @see http://sjsky.iteye.com * @author Michael * */ public class SnmpGetListAsyn { private static int version = SnmpConstants.version1; private static String protocol = "udp"; private static int port = 161; /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; SnmpGetListAsyn tester = new SnmpGetListAsyn(); List<String> oidList = new ArrayList<String>(); oidList.add(".1.3.6.1.2.1.1.1.0"); oidList.add(".1.3.6.1.2.1.1.3.0"); oidList.add(".1.3.6.1.2.1.1.5.0"); // asynchronous tester.snmpAsynGet(ip, community, oidList); } /** * * @param ipAddress * @param community * @param oid */ private void snmpAsynGet(String ipAddress, String community, List<String> oidList) { String address = protocol + ":" + ipAddress + "/" + port; CommunityTarget target = SnmpUtil.createCommunityTarget(address, community, version, 2 * 1000L, 3); DefaultUdpTransportMapping transport = null; Snmp snmp = null; try { WorkerPool threadPool = ThreadPool.create("TestSNMPWorkPool", 2); MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher( threadPool, new MessageDispatcherImpl()); transport = new DefaultUdpTransportMapping(); snmp = new Snmp(dispatcher, transport); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); snmp.listen(); PDU pdu = new PDU(); pdu.setType(PDU.GET); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { ((Snmp) event.getSource()).cancel(event.getRequest(), this); PDU response = event.getResponse(); PDU request = event.getRequest(); System.out.println("[request]:" + request); if (response == null) { System.out.println("[ERROR]: response is null"); } else if (response.getErrorStatus() != 0) { System.out.println("[ERROR]: response status" + response.getErrorStatus() + " Text:" + response.getErrorStatusText()); } else { System.out.println("Received response Success!!!"); for (int i = 0; i < response.size(); i++) { VariableBinding vb = response.get(i); System.out.println(vb); } } } }; snmp.send(pdu, target, null, listener); System.out.println("asynchronous send pdu wait for response..."); } catch (Exception e) { System.out.println("SNMP GetNext Exception:" + e); } } }
[三]、 SnmpWalk.java
package com.michael.snmp4j; import java.io.IOException; 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.Integer32; import org.snmp4j.smi.Null; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; import com.michael.snmp4j.util.SnmpUtil; /** * SnmpWalk * @see http://sjsky.iteye.com * @author Michael * */ public class SnmpWalk { private static int version = SnmpConstants.version1; private static String protocol = "udp"; private static int port = 161; /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; String targetOid = "1.3.6.1.2.1.2.2.1.2"; SnmpWalk tester = new SnmpWalk(); tester.snmpWalk(ip, community, targetOid); } /** * 1)responsePDU == null<br> * 2)responsePDU.getErrorStatus() != 0<br> * 3)responsePDU.get(0).getOid() == null<br> * 4)responsePDU.get(0).getOid().size() < targetOID.size()<br> * 5)targetOID.leftMostCompare(targetOID.size(),responsePDU.get(0).getOid())!=0<br> * 6)Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())<br> * 7)responsePDU.get(0).getOid().compareTo(targetOID) <= 0<br> * @param ipAddress * @param community * @param oid */ @SuppressWarnings("unused") private void snmpWalk(String ip, String community, String targetOid) { String address = protocol + ":" + ip + "/" + port; OID targetOID = new OID(targetOid); PDU requestPDU = new PDU(); requestPDU.setType(PDU.GETNEXT); requestPDU.add(new VariableBinding(targetOID)); CommunityTarget target = SnmpUtil.createCommunityTarget(address, community, version, 2 * 1000L, 3); TransportMapping transport = null; Snmp snmp = null; try { transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); boolean finished = false; while (!finished) { VariableBinding vb = null; ResponseEvent response = snmp.send(requestPDU, target); PDU responsePDU = response.getResponse(); if (null == responsePDU) { System.out.println("responsePDU == null"); finished = true; break; } else { vb = responsePDU.get(0); } // check finish finished = checkWalkFinished(targetOID, responsePDU, vb); if (!finished) { // Dump response. System.out.println("----"+vb.toString()); System.out.println("----"+vb.getOid().toString()); System.out.println("----"+vb.getVariable().toString()); // Set up the variable binding for the next entry. requestPDU.setRequestID(new Integer32(0)); requestPDU.set(0, vb); } } System.out.println("success finish snmp walk!"); } catch (Exception e) { System.out.println("SNMP walk Exception: " + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } if (transport != null) { try { transport.close(); } catch (IOException ex2) { transport = null; } } } } /** * check snmp walk finish * @param resquestPDU * @param targetOID * @param responsePDU * @param vb * @return */ private boolean checkWalkFinished(OID targetOID, PDU responsePDU, VariableBinding vb) { boolean finished = false; if (responsePDU.getErrorStatus() != 0) { System.out.println("responsePDU.getErrorStatus() != 0 "); System.out.println(responsePDU.getErrorStatusText()); finished = true; } else if (vb.getOid() == null) { System.out.println("vb.getOid() == null"); finished = true; } else if (vb.getOid().size() < targetOID.size()) { System.out.println("vb.getOid().size() < targetOID.size()"); finished = true; } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) { System.out.println("targetOID.leftMostCompare() != 0"); finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { System.out .println("Null.isExceptionSyntax(vb.getVariable().getSyntax())"); finished = true; } else if (vb.getOid().compareTo(targetOID) <= 0) { System.out.println("Variable received is not " + "lexicographic successor of requested " + "one:"); System.out.println(vb.toString() + " <= " + targetOID); finished = true; } return finished; } }
[四]、SnmpUtil.java
package michael.snmp.util; import org.snmp4j.CommunityTarget; import org.snmp4j.UserTarget; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OctetString; import org.snmp4j.smi.TcpAddress; import org.snmp4j.smi.UdpAddress; /** * @see http://sjsky.iteye.com * @author michael * */ public class SnmpUtil { public static final int DEFAULT_VERSION = SnmpConstants.version2c; public static final String DEFAULT_PROTOCOL = "udp"; public static final int DEFAULT_PORT = 161; public static final long DEFAULT_TIMEOUT = 3 * 1000L; public static final int DEFAULT_RETRY = 3; /** * 创建共同体对象communityTarget * @param address * @param community * @return CommunityTarget */ public static CommunityTarget createMyDefaultTarget(String address, String community) { Address targetAddress = GenericAddress.parse(address); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(targetAddress); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT);// milliseconds target.setRetries(DEFAULT_RETRY); return target; } /** * 创建共同体对象communityTarget * @param targetAddress * @param community * @param version * @return CommunityTarget */ public static CommunityTarget createCommunityTarget(Address targetAddress, String community, int version) { CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(targetAddress); target.setVersion(version); return target; } /** * 创建共同体对象communityTarget * @param targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createCommunityTarget(Address targetAddress, String community, int version, long timeOut, int retry) { CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(targetAddress); target.setVersion(version); target.setTimeout(timeOut); // milliseconds target.setRetries(retry); return target; } /** * 创建共同体对象communityTarget * @param address * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createCommunityTarget(String address, String community, int version, long timeOut, int retry) { Address targetAddress = GenericAddress.parse(address); return createCommunityTarget(targetAddress, community, version, timeOut, retry); } /** * 创建snmp Address * @param protocol * @param ip * @param port * @return Address */ public static Address createAddress(String protocol, String ip, int port) { String address = protocol + ":" + ip + "/" + port; return GenericAddress.parse(address); } /** * 创建snmp udp Address * @param ip * @param port * @return Address */ public static Address createUdpAddress(String ip, int port) { String address = ip + "/" + port; return new UdpAddress(address); } /** * 创建snmp tcp Address * @param ip * @param port * @return Address */ public static TcpAddress createTcpAddress(String ip, int port) { String address = ip + "/" + port; return new TcpAddress(address); } /** * 创建 UserTarget * @param targetAddress * @param version * @param timeOut * @param level * @param securityName * @return UserTarget */ public static UserTarget createUserTarget(Address targetAddress, int version, long timeOut, int level, String securityName) { UserTarget target = new UserTarget(); target.setAddress(targetAddress); target.setRetries(1); target.setTimeout(timeOut); // milliseconds target.setVersion(version); target.setSecurityLevel(level); target.setSecurityName(new OctetString(securityName)); return target; } /** * 创建 UserTarget * @param address * @param version * @param timeOut * @param level * @param securityName * @return UserTarget */ public static UserTarget createUserTarget(String address, int version, long timeOut, int level, String securityName) { Address targetAddress = GenericAddress.parse(address); return createUserTarget(targetAddress, version, timeOut, level, securityName); } }
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
-----------------------------------分 ------------------------------------隔 ------------------------------------线 --------------------------------------
评论
5 楼
muzi_tao
2012-01-13
为什么不贴上snmp SET的例子呢?
4 楼
sjsky
2011-08-10
iamblackwolf 写道
代码好强大,thanks
3 楼
iamblackwolf
2011-08-10
代码好强大,thanks
2 楼
sjsky
2011-06-17
chen4w 写道
文章简洁扼要,有帮助,谢谢!但为什么不贴SnmpUtil的代码呢?
这个util类里有很多其他的东西,这里的示例中只引用了一个方法,创建共同体SnmpUtil.createCommunityTarget(....),当时懒了点 没有贴的,已把SnmpUtil的代码清理了下,贴出了部分
1 楼
chen4w
2011-06-16
文章简洁扼要,有帮助,谢谢!但为什么不贴SnmpUtil的代码呢?
发表评论
-
SNMP4j实现Trap的示例
2012-07-20 10:52 1777目录: 环境参数 代码示例 详见 ... -
SNMP4j实现同步和异步的WALK的示例
2012-07-19 14:10 1504目录: 环境参数 代码示例 同步WA ... -
SNMP4j实现同步和异步的GET的示例
2012-07-19 14:09 1592目录: 环境参数 代码示例 同步GE ... -
JRobin绘制指定时间段的流量图
2012-06-21 08:39 1461详见: http://www.micmiu.com ... -
SNMP4j之基础介绍
2011-05-08 15:35 3502blog迁移至:http://www ... -
SNMP之JRobin画图
2010-11-18 12:57 2739blog迁移至:http://www.micmiu.c ... -
SNMP之JRobin Core学习
2010-11-15 08:46 3602blog迁移至:http://www.micmiu.c ...
相关推荐
5. **SNMP GET WALK**:GET WALK操作结合了GET和GET NEXT,用于遍历MIB树中的一个分支,获取从某个OID开始的一系列对象值。它是获取大量设备状态信息的有效方式,特别适用于收集表(table)类型的MIB对象,如接口...
### Java实现SNMP获取OID值:深入解析与代码示例 #### SNMP协议简介与OID概念 简单网络管理协议(SNMP)是一种应用层协议,用于收集和组织有关网络设备的信息,如路由器、交换机和服务器等,并对其进行管理。SNMP...
在这份文档中,提到了几个SNMP4J中实现的操作,包括GET、GETNEXT、GETBULK、Walk和SET。 GET请求用于检索单个或多个管理对象实例的值。这是最基本的操作,通常用来获取一个或多个特定的管理信息库(MIB)对象的值。...
Java实现snmp的get和walk代码示例.doc snmp4j.chm snmp.chm SnmpMain.java snmp配置说明.doc snmp协议详细讲解.doc ToolsetAdministratorGuide.pdf ToolsetQuickStartGuide.pdf 常用oids.txt 为什么积分越来越少,...
根据提供的部分代码片段,可以看出这是一个使用Java语言实现SNMP代理的示例。下面将详细解析这段代码的关键部分及其功能。 #### Java实现SNMP的环境搭建 为了使用Java实现SNMP的功能,首先需要引入相关的库。在...
本文将详细介绍SNMP实例,特别是通过Java库snmp4j实现的GET、GETNEXT、SET操作以及TRAP发送和MIB获取。 1. **SNMP基本概念** SNMP由三个主要组件构成:管理站(Manager)、代理(Agent)和管理信息库(MIB)。管理...
在这个项目中,提供的"JAVA基于SNMP网络设备MIB信息采集"可能包含了实现上述功能的Java代码示例。通过学习和理解这些代码,开发者可以更好地掌握如何在Java应用程序中集成SNMP功能,从而实现对网络设备的远程监控和...
SNMP4J是Java开发人员实现SNMP功能的首选工具,因为它提供了完整的SNMPv1、v2c和v3支持,包括Trap处理、Get、Set操作以及Walk机制。在实际应用中,我们可以通过以下步骤使用SNMP4J: 1. **导入SNMP4J库**:首先,在...
压缩包子文件"Check_for_machine_test.java"很可能是一个Java源代码文件,它可能包含了使用NET-SNMP Java库进行SNMP通信的示例代码,比如设置SNMP代理、发送GET或SET请求、遍历MIB树等操作。而"www.pudn.com.txt"...
3. 编程示例:用Python、C++、Java等语言编写的SNMP客户端和服务器端代码示例。 4. 教程和指南:深入浅出地解释SNMP的使用和编程,包括错误处理和最佳实践。 5. 工具集:如snmpwalk、snmpget、snmpset等命令行工具,...
下面是一个简单的 Java 示例代码,演示如何使用 Java 实现 SNMP 的 Get 请求: ```java import java.io.IOException; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import ...
1. **SNMP协议支持**:SNMP4j 提供了对SNMP v1、v2c 和 v3的支持,涵盖了各种操作,如Get、Set、Trap以及Walk(遍历MIB树)等。 2. **PDU(Protocol Data Unit)实现**:SNMP4j 提供了各种PDU类型,如GetRequestPDU...
如果你需要自己编写代码来实现SNMP功能,以下是一段简单的Python示例,使用`pysnmp`库获取网卡描述信息: ```python from pysnmp.hlapi import * errorIndication, errorStatus, errorIndex, varBinds = next( ...