目录:
- 环境参数
- 代码示例
- 同步GET 单个OID
- 同步GET 多个OID
- 异步GET
[一]、环境参数
- jdk 1.6.0_18
- snmp4j 1.11.3
[二]、代码示例
1.同步GET 单个OID(v2c)
代码:SnmpGet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
package com.micmiu.snmp4j.demo1x; import java.io.IOException; 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.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; /** * 演示: GET单个OID值 * * blog http://www.micmiu.com * * @author Michael */ public class SnmpGet { 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 targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createDefault(String ip, String community) { Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT); // milliseconds target.setRetries(DEFAULT_RETRY); return target; } public static void snmpGet(String ip, String community, String oid) { CommunityTarget target = createDefault(ip, community); 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))); DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); System.out.println("-------> 发送PDU <-------"); pdu.setType(PDU.GET); ResponseEvent respEvent = snmp.send(pdu, target); System.out.println("PeerAddress:" + respEvent.getPeerAddress()); PDU response = respEvent.getResponse(); if (response == null) { System.out.println("response is null, request time out"); } else { // Vector<VariableBinding> vbVect = // response.getVariableBindings(); // System.out.println("vb size:" + vbVect.size()); // if (vbVect.size() == 0) { // System.out.println("response vb size is 0 "); // } else { // VariableBinding vb = vbVect.firstElement(); // System.out.println(vb.getOid() + " = " + vb.getVariable()); // } System.out.println("response pdu size is " + response.size()); for (int i = 0; i < response.size(); i++) { VariableBinding vb = response.get(i); System.out.println(vb.getOid() + " = " + vb.getVariable()); } } System.out.println("SNMP GET one OID value finished !"); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP Get Exception:" + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } } } /** * * @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.1.0"; SnmpGet.snmpGet(ip, community, oidval); } } |
运行结果:
1 2 3 4 5 6 7 8 9 |
-------> 发送PDU <------- PeerAddress:192.168.8.254/161 response pdu size is 1 1.3.6.1.2.1.1.1.0 = H3C ICG2000 H3C Comware Platform Software Comware Software Version 5.20, Release 2104P02 Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd. SNMP GET one OID value finished ! |
2.同步GET 多个OID(v2c)
代码:SnmpGetList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
package com.micmiu.snmp4j.demo1x; 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.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; /** * 演示:GET 多个OID值 * * blog http://www.micmiu.com * * @author Michael * */ public class SnmpGetList { 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 targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createDefault(String ip, String community) { Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT); // milliseconds target.setRetries(DEFAULT_RETRY); return target; } /** * * @param ip * @param community * @param oid */ public static void snmpGetList(String ip, String community, List<String> oidList) { CommunityTarget target = SnmpUtil.createDefault(ip, community); Snmp snmp = null; try { PDU pdu = new PDU(); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); transport.listen(); snmp = new Snmp(transport); System.out.println("------->发送消息<-------"); pdu.setType(PDU.GET); ResponseEvent respEvent = snmp.send(pdu, target); System.out.println("PeerAddress:" + respEvent.getPeerAddress()); PDU response = respEvent.getResponse(); if (response == null) { System.out.println("response is null, request time out"); } else { System.out.println("response pdu size is " + response.size()); for (int i = 0; i < response.size(); i++) { VariableBinding vb = response.get(i); System.out.println(vb.getOid() + " = " + vb.getVariable()); } } System.out.println("SNMP GET List OID value finished !"); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP GetList Exception:" + e); } finally { if (snmp != null) { try { snmp.close(); } catch (IOException ex1) { snmp = null; } } } } /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; 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"); SnmpGetList.snmpGetList(ip, community, oidList); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 |
------->发送消息<------- PeerAddress:192.168.8.254/161 response pdu size is 3 1.3.6.1.2.1.1.1.0 = H3C ICG2000 H3C Comware Platform Software Comware Software Version 5.20, Release 2104P02 Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd. 1.3.6.1.2.1.1.3.0 = 39 days, 21:25:37.36 1.3.6.1.2.1.1.5.0 = HX_ICG2000 SNMP GET List OID value finished ! |
3.异步GET (v2c)
代码:SnmpGetAsyn.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
package com.micmiu.snmp4j.demo1x; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; 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.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; /** * 演示:异步GET OID值 * * blog http://www.micmiu.com * * @author Michael * */ public class SnmpGetAsyn { 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 targetAddress * @param community * @param version * @param timeOut * @param retry * @return CommunityTarget */ public static CommunityTarget createDefault(String ip, String community) { Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(DEFAULT_VERSION); target.setTimeout(DEFAULT_TIMEOUT); // milliseconds target.setRetries(DEFAULT_RETRY); return target; } /** * 异步采集信息 * * @param ip * @param community * @param oid */ public static void snmpAsynGetList(String ip, String community, List<String> oidList) { CommunityTarget target = createDefault(ip, community); Snmp snmp = null; try { DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); PDU pdu = new PDU(); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } final CountDownLatch latch = new CountDownLatch(1); 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.getOid() + " = " + vb.getVariable()); } System.out.println("SNMP Asyn GetList OID finished. "); latch.countDown(); } } }; pdu.setType(PDU.GET); snmp.send(pdu, target, null, listener); System.out.println("asyn send pdu wait for response..."); boolean wait = latch.await(30, TimeUnit.SECONDS); System.out.println("latch.await =:" + wait); snmp.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("SNMP Asyn GetList Exception:" + e); } } /** * * @param args */ public static void main(String[] args) { String ip = "192.168.8.254"; String community = "public"; 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"); // 异步采集数据 SnmpGetAsyn.snmpAsynGetList(ip, community, oidList); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
asyn send pdu wait for response... [request]:GET[requestID=1506196554, errorStatus=Success(0), errorIndex=0, VBS[1.3.6.1.2.1.1.1.0 = Null; 1.3.6.1.2.1.1.3.0 = Null; 1.3.6.1.2.1.1.5.0 = Null]] Received response Success! 1.3.6.1.2.1.1.1.0 = H3C ICG2000 H3C Comware Platform Software Comware Software Version 5.20, Release 2104P02 Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd. 1.3.6.1.2.1.1.3.0 = 39 days, 21:26:34.40 1.3.6.1.2.1.1.5.0 = HX_ICG2000 SNMP Asyn GetList OID finished. latch.await =:true |
—–
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/enterprise-app/snmp/snmp4j-get/
2.同步GET 多个OID(v2c)
CommunityTarget target = SnmpUtil.createDefault(ip,community);
出现:
SnmpUtil cannot be resolved
请问SnmpUtil是什么包里面的么?
SnmpUtil 是个工具类 没有放上去,你就替换用这个类中的 createDefault 方法接口