依赖于第三方 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 oidList = new ArrayList();
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 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 oidList = new ArrayList();
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 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
snmp.getMessageDispatcher().addMessageProcessingModel(new
snmp.getMessageDispatcher().addMessageProcessingModel(new
MPv1());
MPv2c());
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) {
event.getSource()).cancel(event.getRequest(), this);
((Snmp)
null");
Text:"
Success!!!");
PDU response = event.getResponse();
PDU request = event.getRequest();
System.out.println("[request]:" + request);
if (response == null) {
System.out.println("[ERROR]: response is
} else if (response.getErrorStatus() != 0) {
System.out.println("[ERROR]: response status"
+ response.getErrorStatus() + "
+ response.getErrorStatusText());
} else {
System.out.println("Received response
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);
}
}