1.下载
首先到官网下载最新的snmp4j 2.2.5
http://www.snmp4j.org/html/download.html
2.一个比较好的起步点是阅读官方的 javadoc,里面有比较详细的说明和示例代码,可以从org.snmp4j包看起。
一个远程系统是通过创建一个Target实例来被snmp4j识别的。
SNMPv1和SNMPv2c必须使用CommunityTarget,SNMPv3则要使用UserTarget。
一条SNMP消息包括消息的载荷,PDU和消息头。简单来说,消息头用Target表示,而PDU则用以下类来表示:
PDUv1 (SNMPv1)
PDU (SNMPv2c)
ScopedPDU (SNMPv3)
消息发送有2种方式:同步和异步。
异步方式需要实现ResponseListener接口,当消息返回时,这个接口的onResponse(ResponseEvent event)方法会被回调。
3.代码
以下代码演示了同步和异步2种方式获取SNMP消息。
本机是Win7的,自带的SNMP协议是v2版的。(Win7不支持v3)
以下是一些OID的解释
1.3.6.1.2.1.1.1 获取CPU和OS信息
1.3.6.1.2.1.2.1.0 网络接口信息描述
1.3.6.1.2.1.25.6.3.1.2 获取安装的软件信息
import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; 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.UdpAddress; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultUdpTransportMapping; public class SnmpTest { public static void main(String[] args) throws Exception { Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161"); TransportMappingtransport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); transport.listen(); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); target.setRetries(2); target.setTimeout(15000); target.setVersion(SnmpConstants.version1); PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1"))); // sysDescr pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.1"))); // ifNumber pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.25.6.3.1.2"))); // Software pdu.setType(PDU.GETNEXT); //发送同步消息 ResponseEvent event = snmp.send(pdu, target); if (event.getResponse() == null) { System.out.println("request timed out"); } else { PDU response = event.getResponse(); System.out.println("Received synchronous response from: " + event.getPeerAddress()); // dump response PDU System.out.println(response.toString()); //遍历并打印得到的信息 VariableBinding[] array = response.toArray(); for (VariableBinding variableBinding : array) { System.out.println(variableBinding.getOid() + "=" + variableBinding.getVariable()); //如果是文字,则取得字节数组并打印出人看得懂的中文 if (variableBinding.getVariable() instanceof OctetString) { OctetString octetString = (OctetString)variableBinding.getVariable(); byte[] value = octetString.getValue(); String softwareName = new String(value, "GB2312"); System.out.println(softwareName); } } } System.out.println(); //发送异步消息 ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { // Always cancel async request when response has been received // otherwise a memory leak is created! Not canceling a request // immediately can be useful when sending a request to a broadcast // address. ((Snmp)event.getSource()).cancel(event.getRequest(), this); PDU response = event.getResponse(); PDU request = event.getRequest(); if (response == null) { System.out.println("Request "+request+" timed out"); } else { System.out.println("Received asynchronous response from: " + event.getPeerAddress()); System.out.println(response + " on request " + request); } } }; snmp.send(pdu, target, null, listener); //等待一会儿,不然还没等异步消息接收到,这里程序就直接退出了。 Thread.sleep(1000); } }
4.运行结果
Received synchronous response from: 127.0.0.1/161 RESPONSE[requestID=1131489612, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1.0 = Hardware: x86 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.1 (Build 7601 Multiprocessor Free); 1.3.6.1.2.1.2.1.0 = 31; 1.3.6.1.2.1.25.6.3.1.2.1 = 57:69:6e:64:6f:77:73:20:c7:fd:b6:af:b3:cc:d0:f2:b0:fc:20:2d:20:47:6f:6f:67:6c:65:2c:20:49:6e:63:2e:20:28:57:69:6e:55:53:42:29:20:41:6e:64:72:6f:69:64:55:73:62:44:65:76:69:63:65:43:6c:61:73:73]] 1.3.6.1.2.1.1.1.0=Hardware: x86 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.1 (Build 7601 Multiprocessor Free) Hardware: x86 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.1 (Build 7601 Multiprocessor Free) 1.3.6.1.2.1.2.1.0=31 1.3.6.1.2.1.25.6.3.1.2.1=57:69:6e:64:6f:77:73:20:c7:fd:b6:af:b3:cc:d0:f2:b0:fc:20:2d:20:47:6f:6f:67:6c:65:2c:20:49:6e:63:2e:20:28:57:69:6e:55:53:42:29:20:41:6e:64:72:6f:69:64:55:73:62:44:65:76:69:63:65:43:6c:61:73:73 Windows 驱动程序包 - Google, Inc. (WinUSB) AndroidUsbDeviceClass Received asynchronous response from: 127.0.0.1/161 RESPONSE[requestID=1131489612, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1.0 = Hardware: x86 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.1 (Build 7601 Multiprocessor Free); 1.3.6.1.2.1.2.1.0 = 31; 1.3.6.1.2.1.25.6.3.1.2.1 = 57:69:6e:64:6f:77:73:20:c7:fd:b6:af:b3:cc:d0:f2:b0:fc:20:2d:20:47:6f:6f:67:6c:65:2c:20:49:6e:63:2e:20:28:57:69:6e:55:53:42:29:20:41:6e:64:72:6f:69:64:55:73:62:44:65:76:69:63:65:43:6c:61:73:73]] on request GETNEXT[requestID=1131489612, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1 = Null; 1.3.6.1.2.1.2.1 = Null; 1.3.6.1.2.1.25.6.3.1.2 = Null]]
5.snmp4j 和 snmp4j-agent
snmp4j可以获取被管节点信息
snmp4j-agent则是写agent端的代理程序的,我们可以自定义一个软件,写一个agent,这样就能被网管软件统一管理了。
另外jdk自带的JMX包也有snmp协议的实现,不过snmp4j可能更方便点。
6.参考资料
使用snmp4j实现Snmp功能 http://blog.csdn.net/clearwater21cn/article/details/2328047 一共3篇