使用Java和SNMP4J實(shí)現(xiàn)SNMP操作完整代碼
引言
SNMP(簡(jiǎn)單網(wǎng)絡(luò)管理協(xié)議)是一種用于網(wǎng)絡(luò)設(shè)備管理的標(biāo)準(zhǔn)協(xié)議。本文將介紹如何使用 Java 和 SNMP4J(一個(gè)開源的 SNMP 實(shí)現(xiàn)庫(kù))進(jìn)行 SNMP 操作。我們將通過(guò)編寫 SnmpUtil 類來(lái)演示如何進(jìn)行 SNMP 初始化、創(chuàng)建 PDU、發(fā)送 SNMP 請(qǐng)求并處理響應(yīng)。
環(huán)境準(zhǔn)備
首先,確保您已經(jīng)添加了 SNMP4J 依賴。可以通過(guò) Maven 或 Gradle 進(jìn)行依賴管理。
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>2.8.6</version>
</dependency>implementation 'org.snmp4j:snmp4j:2.8.6'
SnmpUtil 類概述
以下是編寫 SnmpUtil 類的整體結(jié)構(gòu)。該類提供了靜態(tài)方法來(lái)初始化 SNMP、創(chuàng)建 SNMP 目標(biāo)、創(chuàng)建 PDU、發(fā)送 SNMP 請(qǐng)求和處理響應(yīng)。
類的靜態(tài)初始化
我們?cè)谶@使用靜態(tài)塊來(lái)初始化 SNMP 對(duì)象,確保整個(gè)應(yīng)用程序生命周期中只進(jìn)行一次初始化。
public class SnmpUtil {
private static final Logger log = LoggerFactory.getLogger(SnmpUtil.class);
private static Snmp snmp = null;
static {
try {
initSnmp();
} catch (IOException e) {
log.error("SNMP 初始化失敗", e);
}
}
private static synchronized void initSnmp() throws IOException {
if (snmp == null) {
MessageDispatcher messageDispatcher = new MessageDispatcherImpl();
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), localEngineID, 0);
UsmUser user = new UsmUser(new OctetString("SNMPV3"), AuthSHA.ID, new OctetString("authPassword"),
PrivAES128.ID, new OctetString("privPassword"));
usm.addUser(user.getSecurityName(), user);
messageDispatcher.addMessageProcessingModel(new MPv3(usm));
TransportMapping<?> transportMapping = new DefaultUdpTransportMapping();
snmp = new Snmp(messageDispatcher, transportMapping);
snmp.listen();
}
}
}
創(chuàng)建目標(biāo)
編寫createTarget 方法用于創(chuàng)建 SNMP 目標(biāo),支持 SNMP v1, v2c 和 v3 版本。
private static Target createTarget(int version, String community, String ipAddress, int port) {
Target target = null;
if (!(version == SnmpConstants.version3 || version == SnmpConstants.version2c || version == SnmpConstants.version1)) {
log.error("參數(shù)version異常");
return target;
}
if (version == SnmpConstants.version3) {
target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("SNMPV3"));
} else {
target = new CommunityTarget();
((CommunityTarget) target).setCommunity(new OctetString(community));
if (version == SnmpConstants.version2c) {
target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
}
}
target.setVersion(version);
target.setAddress(GenericAddress.parse("udp:" + ipAddress + "/" + port));
target.setRetries(5);
target.setTimeout(3000);
return target;
}創(chuàng)建 PDU
編寫createPDU 方法用于創(chuàng)建 Protocol Data Unit (PDU),這是 SNMP 消息的基本構(gòu)成單元。
private static PDU createPDU(int version, int type, String oid) {
PDU pdu = null;
if (version == SnmpConstants.version3) {
pdu = new ScopedPDU();
} else {
pdu = new PDUv1();
}
pdu.setType(type);
pdu.add(new VariableBinding(new OID(oid)));
return pdu;
}
private static PDU createPDU(int version, int type, List<String> oids) {
PDU pdu = null;
if (version == SnmpConstants.version3) {
pdu = new ScopedPDU();
} else {
pdu = new PDU();
}
pdu.setType(type);
for (String oid : oids) {
pdu.add(new VariableBinding(new OID(oid)));
}
return pdu;
}發(fā)送 SNMP 請(qǐng)求
編寫snmpWalk 方法用于發(fā)送 GETNEXT 請(qǐng)求,并處理響應(yīng)。此處注意,不一定都是用GETNEXT請(qǐng)求,也可以用GET請(qǐng)求,兩者區(qū)別故名思義,一個(gè)是獲取下一條oid,一個(gè)是獲取自己本身
public static PDU snmpWalk(String ipAddr, int port, int version, String community, String oid) {
try {
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = createPDU(version, PDU.GETNEXT, oid);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
return response;
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
return new PDU();
}
}
public static PDU snmpWalk(String ipAddr, int port, int version, String community, List<String> oids) {
try {
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = createPDU(version, PDU.GETNEXT, oids);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
return response;
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
return new PDU();
}
}處理 SNMP 響應(yīng)
編寫snmpWalkAll 和 snmpWalkSegment 方法用于處理連續(xù)的 SNMP 響應(yīng),直到到達(dá) MIB 的末尾。
public static List<VariableBinding> snmpWalkAll(String ipAddr, int port, int version, String community, String startOid) {
List<VariableBinding> results = new ArrayList<>();
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(startOid)));
while (true) {
try {
pdu.setType(PDU.GETNEXT);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response != null && response.size() > 0 && response.getErrorStatus() == 0) {
if (response.get(0).getVariable().toString().equals("endOfMibView")) {
break;
}
results.addAll(response.getVariableBindings());
pdu.setRequestID(null); // Reset request ID for the next request
pdu.remove(0); // Remove the old request
pdu.add(response.get(0)); // Add the new request based on the last response
} else {
break;
}
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
break;
}
}
return results;
}
// ...其他代碼見(jiàn)完整代碼
完整代碼
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.*;
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;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SnmpUtil {
private static final Logger log = LoggerFactory.getLogger(SnmpUtil.class);
private static Snmp snmp = null; // 將共享資源的訪問(wèn)控制為靜態(tài)初始化
static {
// 將 SNMP 對(duì)象的初始化放在靜態(tài)塊中,確保只初始化一次
try {
initSnmp();
} catch (IOException e) {
log.error("SNMP 初始化失敗", e); // 記錄初始化失敗的異常信息
}
}
private static synchronized void initSnmp() throws IOException { // 添加 synchronized 關(guān)鍵字保證線程安全
if (snmp == null) {
MessageDispatcher messageDispatcher = new MessageDispatcherImpl();
messageDispatcher.addMessageProcessingModel(new MPv1());
messageDispatcher.addMessageProcessingModel(new MPv2c());
OctetString localEngineID = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), localEngineID, 0);
UsmUser user = new UsmUser(new OctetString("SNMPV3"), AuthSHA.ID, new OctetString("authPassword"),
PrivAES128.ID, new OctetString("privPassword"));
usm.addUser(user.getSecurityName(), user);
messageDispatcher.addMessageProcessingModel(new MPv3(usm));
TransportMapping<?> transportMapping = new DefaultUdpTransportMapping();
snmp = new Snmp(messageDispatcher, transportMapping);
snmp.listen();
}
}
private static Target createTarget(int version, String community, String ipAddress, int port) {
Target target = null;
if (!(version == SnmpConstants.version3 || version == SnmpConstants.version2c || version == SnmpConstants.version1)) {
log.error("參數(shù)version異常"); // 保持錯(cuò)誤日志記錄
return target;
}
if (version == SnmpConstants.version3) {
target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("SNMPV3"));
} else {
target = new CommunityTarget();
((CommunityTarget) target).setCommunity(new OctetString(community));
if (version == SnmpConstants.version2c) {
target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
}
}
target.setVersion(version);
target.setAddress(GenericAddress.parse("udp:" + ipAddress + "/" + port));
target.setRetries(5);
target.setTimeout(3000);
return target;
}
/**
* 創(chuàng)建 PDU 對(duì)象
* @param version
* @param type
* @param oid
* @return
*/
private static PDU createPDU(int version, int type, String oid) {
PDU pdu = null;
if (version == SnmpConstants.version3) {
pdu = new ScopedPDU();
} else {
pdu = new PDUv1();
}
pdu.setType(type);
pdu.add(new VariableBinding(new OID(oid)));
return pdu;
}
/**
* 創(chuàng)建 PDU 對(duì)象
* @param version
* @param type
* @param oids
* @return
*/
private static PDU createPDU(int version, int type, List<String> oids) {
PDU pdu = null;
if (version == SnmpConstants.version3) {
pdu = new ScopedPDU();
} else {
pdu = new PDU();
}
pdu.setType(type);
for (String oid : oids) {
pdu.add(new VariableBinding(new OID(oid)));
}
return pdu;
}
/**
* 獲取 SNMP 響應(yīng)
* @param ipAddr
* @param port
* @param version
* @param community
* @param oid
* @return
*/
public static PDU snmpWalk(String ipAddr, int port, int version, String community, String oid) {
try {
// 由于 snmp 對(duì)象已在靜態(tài)塊中初始化,這里不再需要調(diào)用 initSnmp
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = createPDU(version, PDU.GETNEXT, oid); // 保持內(nèi)部字符串不變
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
return response;
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e); // 增加異常日志記錄
return new PDU(); // 在異常情況下仍然返回一個(gè)新的 PDU 對(duì)象,但已記錄錯(cuò)誤信息
}
}
/**
* 獲取 SNMP 響應(yīng)
* @param ipAddr
* @param port
* @param version
* @param community
* @param oids
* @return
*/
public static PDU snmpWalk(String ipAddr, int port, int version, String community, List<String> oids) {
try {
// 由于 snmp 對(duì)象已在靜態(tài)塊中初始化,這里不再需要調(diào)用 initSnmp
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = createPDU(version, PDU.GETNEXT, oids); // 保持內(nèi)部字符串不變
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
return response;
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e); // 增加異常日志記錄
return new PDU(); // 在異常情況下仍然返回一個(gè)新的 PDU 對(duì)象,但已記錄錯(cuò)誤信息
}
}
/**
* 獲取 SNMP 響應(yīng)
* @param ipAddr
* @param port
* @param version
* @param community
* @param startOid
* @return
*/
public static List<VariableBinding> snmpWalkAll(String ipAddr, int port, int version, String community, String startOid) {
List<VariableBinding> results = new ArrayList<>();
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(startOid)));
while (true) {
try {
pdu.setType(PDU.GETNEXT);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response != null && response.size() > 0 && response.getErrorStatus() == 0) {
if (response.get(0).getVariable().toString().equals("endOfMibView")) {
break;
}
results.addAll(response.getVariableBindings());
pdu.setRequestID(null); // Reset request ID for the next request
pdu.remove(0); // Remove the old request
pdu.add(response.get(0)); // Add the new request based on the last response
} else {
break;
}
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
break;
}
}
return results;
}
/**
* 獲取 SNMP 響應(yīng)
* @param ipAddr
* @param port
* @param version
* @param community
* @param startOid
* @return
*/
public static List<VariableBinding> snmpWalkSegment(String ipAddr, int port, int version, String community, String startOid) {
List<VariableBinding> results = new ArrayList<>();
Target target = createTarget(version, community, ipAddr, port);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(startOid)));
while (true) {
try {
pdu.setType(PDU.GETNEXT);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response != null && response.size() > 0 && response.getErrorStatus() == 0) {
VariableBinding vb = response.get(0);
results.add(vb);
// Check if we have reached the end of the branch
if (!vb.getOid().startsWith(new OID(startOid)) || vb.getVariable().toString().equals("endOfMibView")) {
break;
}
pdu.setRequestID(null); // Reset request ID for the next request
pdu.remove(0); // Remove the old request
pdu.add(vb); // Add the new request based on the last response
System.out.println("OID: " + vb.getOid() + ", Value: " + vb.getVariable());
} else {
break;
}
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
break;
}
}
return results;
}
public static List<VariableBinding> snmpWalkSegment(String ipAddr, int port, int version, String community, List<String> startOids) {
List<VariableBinding> results = new ArrayList<>();
Target target = createTarget(version, community, ipAddr, port);
for(String startOid: startOids){
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(startOid)));
while (true) {
try {
pdu.setType(PDU.GETNEXT);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response != null && response.size() > 0 && response.getErrorStatus() == 0) {
VariableBinding vb = response.get(0);
results.add(vb);
// Check if we have reached the end of the branch
if (!vb.getOid().startsWith(new OID(startOid)) || vb.getVariable().toString().equals("endOfMibView")) {
break;
}
pdu.setRequestID(null); // Reset request ID for the next request
pdu.remove(0); // Remove the old request
pdu.add(vb); // Add the new request based on the last response
System.out.println("OID: " + vb.getOid() + ", Value: " + vb.getVariable());
} else {
break;
}
} catch (IOException e) {
log.error("SNMP 發(fā)送請(qǐng)求失敗", e);
break;
}
}
}
return results;
}
public static void main(String[] args) {
PDU response = SnmpUtil.snmpWalk("127.0.0.1", 161, SnmpConstants.version2c, "public", ".1");
if (response != null && response.getErrorStatus() == 0) {
for (VariableBinding vb : response.getVariableBindings()) {
System.out.println("OID: " + vb.getOid() + ", Value: " + vb.getVariable());
}
} else {
System.out.println("Error in response: " + response.getErrorStatusText());
}
}
}
總結(jié)
通過(guò)本文的講解,您應(yīng)該已經(jīng)掌握了如何使用 Java 和 SNMP4J 庫(kù)進(jìn)行 SNMP 操作。我們介紹了 SnmpUtil 類的設(shè)計(jì)和實(shí)現(xiàn),包括 SNMP 初始化、創(chuàng)建目標(biāo)、創(chuàng)建 PDU、發(fā)送 SNMP 請(qǐng)求和處理響應(yīng)等內(nèi)容。希望這篇教程能夠幫助您更好地理解和使用 SNMP4J 進(jìn)行網(wǎng)絡(luò)設(shè)備管理
到此這篇關(guān)于使用Java和SNMP4J實(shí)現(xiàn)SNMP操作的文章就介紹到這了,更多相關(guān)Java和SNMP4J實(shí)現(xiàn)SNMP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)快速排序過(guò)程分析
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)快速排序過(guò)程分析,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
JAVA代碼實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁(yè)查詢
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)MongoDB動(dòng)態(tài)條件之分頁(yè)查詢,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
基于spring?data?jpa?@query返回map的踩坑記錄
這篇文章主要介紹了基于spring?data?jpa?@query返回map的踩坑記錄,具有很好的參考價(jià)值,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java面試題沖刺第二十六天--實(shí)戰(zhàn)編程2
這篇文章主要為大家分享了最有價(jià)值的三道java實(shí)戰(zhàn)編程的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-08-08
解析SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能
這篇文章主要介紹了SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能,AOP面向切面編程,通過(guò)為目標(biāo)類織入切面的方式,實(shí)現(xiàn)對(duì)目標(biāo)類功能的增強(qiáng),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問(wèn)題
這篇文章主要介紹了解決javaBean規(guī)范導(dǎo)致json傳參首字母大寫將永遠(yuǎn)獲取不到問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
@Valid注解的作用及@Valid注解與@Validated的區(qū)別
這篇文章主要介紹了@Valid注解的作用及@Valid注解與@Validated的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

