基于snmp4j,已经有很多示例;
本例子是作者原创文章,创作不易,积极鼓励;
SNMP 是专门设计用于在 IP 网络管理网络节点(服务器、工作站、路由器、交换机及HUBS等)的一种标准协议,它是一种应用层协议。
JAVA如果想使用SNMP协议,则可以通过snmp4j-2.4.3.jar实现,下面通过代码简单介绍下这个jar包的简单使用;
package com.mysnmp;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Snmp4jTool {
public String community = "public";
public String communityPort = "161";
public int communityVersion = 1;
private Snmp snmp = null;
private Address targetAddress = null;
private String host = null;
private CommunityTarget target = new CommunityTarget();
private PDU pdu = new PDU();
private static Integer GETBULK = -91;
private static Integer GET = -96;
private static Integer GETNEXT = -95;
public Snmp4jTool() {
}
public Snmp4jTool(String oid, String host_temp, String port_temp, String community_temp, Integer getType) {
this.init(host_temp, port_temp, community_temp);
this.setTarget();
this.setPDU(oid, getType);
}
public static SnmpResult getOid(String oid, String host_temp, String port_temp, String community_temp) {
Snmp4jTool tool = new Snmp4jTool(oid, host_temp, port_temp, community_temp, GET);
return tool.processResponseSingle(oid);
}
private SnmpResult processResponseSingle(String oid) {
SnmpResult res = new SnmpResult();
ResponseEvent event = null;
try {
event = this.snmp.send(this.pdu, this.target);
} catch (IOException var8) {
var8.printStackTrace();
}
PDU pdu_resp = event.getResponse();
List<VariableBinding> list = pdu_resp.getBindingList(new OID(oid));
if (!UtilValidator.isEmpty(list)) {
VariableBinding vb = (VariableBinding)list.get(0);
SnmpResult sr = new SnmpResult(vb.getOid().toString(), vb.toValueString());
return sr;
} else {
return res;
}
}
public static List<SnmpResult> getOidSubtree(String oid, String host_temp, String port_temp, String community_temp) {
new ArrayList();
Snmp4jTool tool = new Snmp4jTool(oid, host_temp, port_temp, community_temp, GETNEXT);
List<SnmpResult> list = tool.processResponse(oid);
filterList(list);
return list;
}
private static void filterList(List<SnmpResult> list) {
for(int i = 0; i < list.size(); ++i) {
for(int j = list.size() - 1; j > i; --j) {
SnmpResult jo = (SnmpResult)list.get(j);
SnmpResult io = (SnmpResult)list.get(i);
String oidjO = jo.getOid();
String valjO = jo.getValue();
String oidiO = io.getOid();
String valiO = io.getValue();
if (!UtilValidator.isEmpty(oidjO) && !UtilValidator.isEmpty(valjO) && !UtilValidator.isEmpty(oidiO) && !UtilValidator.isEmpty(valiO) && oidjO.equals(oidiO) && valiO.equals(valjO)) {
list.remove(j);
}
}
}
}
private List<SnmpResult> processResponse(String oid) {
ArrayList res = new ArrayList();
while(true) {
ResponseEvent event = null;
try {
event = this.snmp.send(this.pdu, this.target);
} catch (IOException var8) {
var8.printStackTrace();
}
PDU pdu_resp = event.getResponse();
if (UtilValidator.isEmpty(pdu_resp)) {
break;
}
List<VariableBinding> list = pdu_resp.getBindingList(new OID(oid));
if (UtilValidator.isEmpty(list)) {
break;
}
VariableBinding vb = (VariableBinding)list.get(0);
SnmpResult sr = new SnmpResult(vb.getOid().toString(), vb.toValueString());
res.add(sr);
this.pdu.set(0, vb);
}
return res;
}
private void setPDU(String oid, Integer pdu_type) {
this.pdu.add(new VariableBinding(new OID(oid)));
this.pdu.setType(pdu_type);
}
private void setTarget() {
this.target.setCommunity(new OctetString(this.community));
this.target.setAddress(this.targetAddress);
this.target.setRetries(2);
this.target.setTimeout(1500L);
this.target.setVersion(this.communityVersion);
}
private void init(String host_temp, String port_temp, String community_temp) {
this.host = host_temp;
this.communityPort = UtilValidator.isEmpty(port_temp) ? this.communityPort : port_temp;
this.community = UtilValidator.isEmpty(community_temp) ? this.community : community_temp;
String udp = FormatString.append(new Object[]{"udp:", this.host, "/", this.communityPort});
this.targetAddress = GenericAddress.parse(udp);
try {
TransportMapping transport = new DefaultUdpTransportMapping();
this.snmp = new Snmp(transport);
transport.listen();
} catch (IOException var6) {
var6.printStackTrace();
} catch (Exception var7) {
var7.printStackTrace();
}
}
public static void main(String[] args) {
List<SnmpResult> list = getOidSubtree(".1.3.6.1.2.1.2.2.1.1", "127.0.0.1", "161", "public");
Iterator var3 = list.iterator();
while(var3.hasNext()) {
SnmpResult sr = (SnmpResult)var3.next();
System.out.println(sr.getOid() + "=" + sr.getValue());
}
}
}
该代码实现了
public static List<SnmpResult> getOidSubtree(String oid, String host_temp, String port_temp, String community_temp) {}
该方法的主要作用是什么呢?就是给出OID,获取树下的所有列表。