目录:
[一]、环境参数
- jdk 1.6.0_18
- snmp4j 1.11.3
[二]、代码示例
1.同步WALK(v2c)
代码:SnmpWalk.java
1 | package com.micmiu.snmp4j.demo1x; |
3 | import java.io.IOException; |
5 | import org.snmp4j.CommunityTarget; |
8 | import org.snmp4j.TransportMapping; |
9 | import org.snmp4j.event.ResponseEvent; |
10 | import org.snmp4j.mp.SnmpConstants; |
11 | import org.snmp4j.smi.Address; |
12 | import org.snmp4j.smi.GenericAddress; |
13 | import org.snmp4j.smi.Integer32; |
14 | import org.snmp4j.smi.Null; |
15 | import org.snmp4j.smi.OID; |
16 | import org.snmp4j.smi.OctetString; |
17 | import org.snmp4j.smi.VariableBinding; |
18 | import org.snmp4j.transport.DefaultUdpTransportMapping; |
27 | public class SnmpWalk { |
29 | public static final int DEFAULT_VERSION = SnmpConstants.version2c; |
30 | public static final String DEFAULT_PROTOCOL = "udp" ; |
31 | public static final int DEFAULT_PORT = 161 ; |
32 | public static final long DEFAULT_TIMEOUT = 3 * 1000L; |
33 | public static final int DEFAULT_RETRY = 3 ; |
38 | * @param targetAddress |
43 | * @return CommunityTarget |
45 | public static CommunityTarget createDefault(String ip, String community) { |
46 | Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip |
47 | + "/" + DEFAULT_PORT); |
48 | CommunityTarget target = new CommunityTarget(); |
49 | target.setCommunity( new OctetString(community)); |
50 | target.setAddress(address); |
51 | target.setVersion(DEFAULT_VERSION); |
52 | target.setTimeout(DEFAULT_TIMEOUT); // milliseconds |
53 | target.setRetries(DEFAULT_RETRY); |
62 | public static void snmpWalk(String ip, String community, String targetOid) { |
64 | CommunityTarget target = SnmpUtil.createDefault(ip, community); |
65 | TransportMapping transport = null ; |
68 | transport = new DefaultUdpTransportMapping(); |
69 | snmp = new Snmp(transport); |
73 | OID targetOID = new OID(targetOid); |
74 | pdu.add( new VariableBinding(targetOID)); |
76 | boolean finished = false ; |
77 | System.out.println( "----> demo start <----" ); |
79 | VariableBinding vb = null ; |
80 | ResponseEvent respEvent = snmp.getNext(pdu, target); |
82 | PDU response = respEvent.getResponse(); |
84 | if ( null == response) { |
85 | System.out.println( "responsePDU == null" ); |
92 | finished = checkWalkFinished(targetOID, pdu, vb); |
94 | System.out.println( "==== walk each vlaue :" ); |
95 | System.out.println(vb.getOid() + " = " + vb.getVariable()); |
97 | // Set up the variable binding for the next entry. |
98 | pdu.setRequestID( new Integer32( 0 )); |
101 | System.out.println( "SNMP walk OID has finished." ); |
105 | System.out.println( "----> demo end <----" ); |
106 | } catch (Exception e) { |
108 | System.out.println( "SNMP walk Exception: " + e); |
113 | } catch (IOException ex1) { |
122 | * 1)responsePDU == null<br> |
123 | * 2)responsePDU.getErrorStatus() != 0<br> |
124 | * 3)responsePDU.get(0).getOid() == null<br> |
125 | * 4)responsePDU.get(0).getOid().size() < targetOID.size()<br> |
126 | * 5)targetOID.leftMostCompare(targetOID.size(),responsePDU.get(0).getOid()) |
128 | * 6)Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())<br> |
129 | * 7)responsePDU.get(0).getOid().compareTo(targetOID) <= 0<br> |
137 | private static boolean checkWalkFinished(OID targetOID, PDU pdu, |
138 | VariableBinding vb) { |
139 | boolean finished = false ; |
140 | if (pdu.getErrorStatus() != 0 ) { |
141 | System.out.println( "[true] responsePDU.getErrorStatus() != 0 " ); |
142 | System.out.println(pdu.getErrorStatusText()); |
144 | } else if (vb.getOid() == null ) { |
145 | System.out.println( "[true] vb.getOid() == null" ); |
147 | } else if (vb.getOid().size() < targetOID.size()) { |
148 | System.out.println( "[true] vb.getOid().size() < targetOID.size()" ); |
150 | } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0 ) { |
151 | System.out.println( "[true] targetOID.leftMostCompare() != 0" ); |
153 | } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { |
155 | .println( "[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())" ); |
157 | } else if (vb.getOid().compareTo(targetOID) <= 0 ) { |
158 | System.out.println( "[true] Variable received is not " |
159 | + "lexicographic successor of requested " + "one:" ); |
160 | System.out.println(vb.toString() + " <= " + targetOID); |
171 | public static void main(String[] args) { |
172 | String ip = "192.168.8.254" ; |
173 | String community = "public" ; |
174 | // 1.3.6.1.2.1.2.2.1.2 |
175 | String targetOid = ".1.3.6.1.2.1.1" ; |
176 | SnmpWalk.snmpWalk(ip, community, targetOid); |
运行结果:
----> demo start <----==== walk each vlaue :1.3.6.1.2.1.1.1.0 = H3C ICG2000H3C Comware Platform SoftwareComware Software Version 5.20, Release 2104P02Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd.==== walk each vlaue :1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.25506.1.287==== walk each vlaue :1.3.6.1.2.1.1.3.0 = 39 days, 21:47:28.41==== walk each vlaue :1.3.6.1.2.1.1.4.0 = R&D Hangzhou, Hangzhou H3C Technologies Co., Ltd.==== walk each vlaue :1.3.6.1.2.1.1.5.0 = HX_ICG2000==== walk each vlaue :1.3.6.1.2.1.1.6.0 = Hangzhou, China==== walk each vlaue :1.3.6.1.2.1.1.7.0 = 78[true] targetOID.leftMostCompare() != 0SNMP walk OID has finished.----> demo end <----
2.异步WALK(v2c)
代码:SnmpWalkAsyn.java
1 | package com.micmiu.snmp4j.demo1x; |
3 | import java.util.ArrayList; |
5 | import java.util.concurrent.CountDownLatch; |
6 | import java.util.concurrent.TimeUnit; |
8 | import org.snmp4j.CommunityTarget; |
10 | import org.snmp4j.Snmp; |
11 | import org.snmp4j.event.ResponseEvent; |
12 | import org.snmp4j.event.ResponseListener; |
13 | import org.snmp4j.mp.SnmpConstants; |
14 | import org.snmp4j.smi.Address; |
15 | import org.snmp4j.smi.GenericAddress; |
16 | import org.snmp4j.smi.Integer32; |
17 | import org.snmp4j.smi.Null; |
18 | import org.snmp4j.smi.OID; |
19 | import org.snmp4j.smi.OctetString; |
20 | import org.snmp4j.smi.VariableBinding; |
21 | import org.snmp4j.transport.DefaultUdpTransportMapping; |
31 | public class SnmpWalkAsyn { |
33 | public static final int DEFAULT_VERSION = SnmpConstants.version2c; |
34 | public static final String DEFAULT_PROTOCOL = "udp" ; |
35 | public static final int DEFAULT_PORT = 161 ; |
36 | public static final long DEFAULT_TIMEOUT = 3 * 1000L; |
37 | public static final int DEFAULT_RETRY = 3 ; |
42 | * @param targetAddress |
47 | * @return CommunityTarget |
49 | public static CommunityTarget createDefault(String ip, String community) { |
50 | Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip |
51 | + "/" + DEFAULT_PORT); |
52 | CommunityTarget target = new CommunityTarget(); |
53 | target.setCommunity( new OctetString(community)); |
54 | target.setAddress(address); |
55 | target.setVersion(DEFAULT_VERSION); |
56 | target.setTimeout(DEFAULT_TIMEOUT); // milliseconds |
57 | target.setRetries(DEFAULT_RETRY); |
68 | public static void snmpAsynWalk(String ip, String community, String oid) { |
69 | final CommunityTarget target = createDefault(ip, community); |
72 | System.out.println( "----> demo start <----" ); |
74 | DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); |
75 | snmp = new Snmp(transport); |
78 | final PDU pdu = new PDU(); |
79 | final OID targetOID = new OID(oid); |
80 | final CountDownLatch latch = new CountDownLatch( 1 ); |
81 | pdu.add( new VariableBinding(targetOID)); |
83 | ResponseListener listener = new ResponseListener() { |
84 | public void onResponse(ResponseEvent event) { |
85 | ((Snmp) event.getSource()).cancel(event.getRequest(), this ); |
88 | PDU response = event.getResponse(); |
89 | // PDU request = event.getRequest(); |
90 | // System.out.println("[request]:" + request); |
91 | if (response == null ) { |
92 | System.out.println( "[ERROR]: response is null" ); |
93 | } else if (response.getErrorStatus() != 0 ) { |
94 | System.out.println( "[ERROR]: response status" |
95 | + response.getErrorStatus() + " Text:" |
96 | + response.getErrorStatusText()); |
99 | .println( "Received Walk response value :" ); |
100 | VariableBinding vb = response.get( 0 ); |
102 | boolean finished = checkWalkFinished(targetOID, |
105 | System.out.println(vb.getOid() + " = " |
107 | pdu.setRequestID( new Integer32( 0 )); |
109 | ((Snmp) event.getSource()).getNext(pdu, target, |
113 | .println( "SNMP Asyn walk OID value success !" ); |
117 | } catch (Exception e) { |
125 | snmp.getNext(pdu, target, null , listener); |
126 | System.out.println( "pdu 已发送,等到异步处理结果..." ); |
128 | boolean wait = latch.await( 30 , TimeUnit.SECONDS); |
129 | System.out.println( "latch.await =:" + wait); |
132 | System.out.println( "----> demo end <----" ); |
133 | } catch (Exception e) { |
135 | System.out.println( "SNMP Asyn Walk Exception:" + e); |
140 | private static boolean checkWalkFinished(OID walkOID, PDU pdu, |
141 | VariableBinding vb) { |
142 | boolean finished = false ; |
143 | if (pdu.getErrorStatus() != 0 ) { |
144 | System.out.println( "[true] pdu.getErrorStatus() != 0 " ); |
145 | System.out.println(pdu.getErrorStatusText()); |
147 | } else if (vb.getOid() == null ) { |
148 | System.out.println( "[true] vb.getOid() == null" ); |
150 | } else if (vb.getOid().size() < walkOID.size()) { |
151 | System.out.println( "[true] vb.getOid().size() < targetOID.size()" ); |
153 | } else if (walkOID.leftMostCompare(walkOID.size(), vb.getOid()) != 0 ) { |
154 | System.out.println( "[true] targetOID.leftMostCompare() != 0" ); |
156 | } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { |
158 | .println( "[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())" ); |
160 | } else if (vb.getOid().compareTo(walkOID) <= 0 ) { |
161 | System.out.println( "[true] vb.getOid().compareTo(walkOID) <= 0 " ); |
172 | public static void main(String[] args) { |
174 | String ip = "192.168.8.254" ; |
175 | String community = "public" ; |
177 | List<String> oidList = new ArrayList<String>(); |
178 | oidList.add( ".1.3.6.1.2.1.1.1.0" ); |
179 | oidList.add( ".1.3.6.1.2.1.1.3.0" ); |
180 | oidList.add( ".1.3.6.1.2.1.1.5.0" ); |
182 | SnmpWalkAsyn.snmpAsynWalk(ip, community, "1.3.6.1.2.1.1" ); |
运行结果:
----> demo start <----pdu 已发送,等到异步处理结果...Received Walk response value :1.3.6.1.2.1.1.1.0 = H3C ICG2000H3C Comware Platform SoftwareComware Software Version 5.20, Release 2104P02Copyright(c) 2004-2010 Hangzhou H3C Technologies Co., Ltd.Received Walk response value :1.3.6.1.2.1.1.2.0 = 1.3.6.1.4.1.25506.1.287Received Walk response value :1.3.6.1.2.1.1.3.0 = 39 days, 21:52:48.80Received Walk response value :1.3.6.1.2.1.1.4.0 = R&D Hangzhou, Hangzhou H3C Technologies Co., Ltd.Received Walk response value :1.3.6.1.2.1.1.5.0 = HX_ICG2000Received Walk response value :1.3.6.1.2.1.1.6.0 = Hangzhou, ChinaReceived Walk response value :1.3.6.1.2.1.1.7.0 = 78Received Walk response value :[true] targetOID.leftMostCompare() != 0SNMP Asyn walk OID value success !latch.await =:true----> demo end <----
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。