問題
我在將 SNMP4J 配置為簡單的 Get-Request 的 SNMPv3 管理器時遇到了一些麻煩。使用 net-snmp 和 SNMP4J,SNMPv2 作業正常。
設定
在我的本地機器上,我有一個正在運行的 snmp 守護程式(通過 net-snmp)。
zetts@zetts_x1_carbon~: cat /etc/snmp/snmpd.conf
rouser v3_ro_user priv .1
rocommunity start123
# sec.name source community
com2sec local localhost public
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
view all included .1 80
access MyROGroup "" any noauth exact all none none
access MyRWGroup "" any noauth exact all all none
使用 net-snmp 的 snmpget 按預期作業:
zetts@zetts_x1_carbon~: snmpget -v 3 -l authPriv -u v3_ro_user -a sha -A myAuthPw -x aes -X myPrivPw localhost .1.3.6.1.2.1.1.1.0
SNMPv2-MIB::sysDescr.0 = STRING: Linux zettsx1carbon 5.10.105-1-MANJARO #1 SMP PREEMPT Fri Mar 11 14:12:33 UTC 2022 x86_64
我的 SNMP4J 實作如下所示:
public static void get() throws IOException {
SnmpBuilder snmpBuilder = new SnmpBuilder();
Snmp snmp = snmpBuilder.udp().v3().usm().build();
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
byte[] targetEngineID = snmp.discoverAuthoritativeEngineID(targetAddress, 1000);
if (targetEngineID != null) {
System.out.println("Trying with " authPro.toString());
TargetBuilder<?> targetBuilder = snmpBuilder.target(targetAddress);
Target<?> target = targetBuilder
.user("v3_ro_user", targetEngineID)
.auth(TargetBuilder.AuthProtocol.hmac192sha256).authPassphrase("myAuthPw")
.priv(TargetBuilder.PrivProtocol.aes128).privPassphrase("myPrivPw")
.done()
.timeout(1500).retries(1)
.build();
target.setVersion(SnmpConstants.version3);
PDU pdu = targetBuilder.pdu().type(PDU.GET).contextName("").build();
pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.1.0")));
SnmpCompletableFuture snmpRequestFuture = SnmpCompletableFuture.send(snmp, target, pdu);
try {
List<VariableBinding> vbs = snmpRequestFuture.get().getAll();
System.out.println("Received: " snmpRequestFuture.getResponseEvent().getResponse());
} catch (ExecutionException | InterruptedException ex) {
System.err.println("Request failed: " ex.getCause().getMessage());
}
} else {
System.err.println("Timeout on engine ID discovery for " targetAddress ", GET not sent.");
}
snmp.close();
}
我有時會使用非流利的語法(例如target.setVersion()),因為相應的流利方法不起作用。
分析
SNMP4J 運行結果:Request failed: SNMP Request timed out
我使用 tcpdump 捕獲了 localhost UDP 埠 161 流量。可以看到來自 net-snmp 的前 4 個用于 snmpget 的資料包(成功),然后是來自 SNMP4J 的 4 個資料包。看起來 EngineID 發現似乎也適用于 SNMP4J,但下一個資料包中的 msgAuthenticationParameters 比相應的 net-snmp 請求資料包中的要長得多。然后,SNMP4J 請求會以 OID “1.3.6.1.6.3.15.1.1.5.0” (oidUsmStatsWrongDigests) 的報告得到答復。
SNMP 守護行程日志:
zetts@zetts_x1_carbon~: cat snmpd.log
NET-SNMP version 5.9.1
Authentication failed for v3_ro_user
所以看起來是認證問題,但是用戶名和密碼肯定是一樣的,而且是正確的。authProtocol 有問題嗎?僅指定“SHA”時 net-snmp 使用哪個版本?
有人可以提供進一步的提示在哪里尋找根本原因嗎?
謝謝你和最好的問候,塞巴斯蒂安
uj5u.com熱心網友回復:
我解決了,最后是SHA版本問題:
net-snmp默認使用 SHA-1,所以我TargetBuilder.AuthProtocol.hmac192sha256改為TargetBuilder.AuthProtocol.sha1.
但是,現在執行立即失敗:
SNMP error Authorization error on index 0
此外,Net-SNMP 守護程式日志為空。
在除錯 SNMP4J 執行并閱讀其檔案內檔案后,我發現SHA-1協議必須顯式添加到 Snmp 實體,因為 SNMP4J 認為它不安全。在 Snmp 構建鏈中添加.securityProtocols(SecurityProtocols.SecurityProtocolSet.maxCompatibility)before現在可以解決所有問題。.v3()
uj5u.com熱心網友回復:
我不熟悉 SNMP4J,但是這段代碼可能需要定義安全級別,authPriv或者 SNMP4J 參考它:
Target<?> target = targetBuilder
.user("v3_ro_user", targetEngineID)
.auth(TargetBuilder.AuthProtocol.hmac192sha256).authPassphrase("myAuthPw")
.priv(TargetBuilder.PrivProtocol.aes128).privPassphrase("myPrivPw")
.done()
.timeout(1500).retries(1)
.build();
target.setVersion(SnmpConstants.version3);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/451745.html
