java - 当 net-snmp get 返回响应事件时,为什么 snmp 代理不接受 snmp4j get 上的请求
问题描述
我正在尝试编写我的第一个 snmp4j 客户端。我有一个在 192.168.60.105 上运行的代理。使用 net-snmp 我可以查询 OID 并获得结果。使用 smnp4j,snmp get 的响应事件返回空响应和空错误。我认为消息超时,但我不知道为什么。
我使用 net-snmp 得到结果
jgaer@ljgaer2_~: snmpget 192.168.60.105 .1.3.6.1.4.1.27675.20.5.2.0
CW-NET-STG-SVR-MIB::cwNetStgSvrProvisioningEnable.0 = Hex-STRING: 00 00 00 00
我尝试过使用更长的超时时间和更多的重试次数,并且返回时间更长。这就是为什么我认为我正在超时。我只是不明白为什么。我还期望如果在超时时返回 responseEvent,则错误将表明这一点。我试过使用 verions1。使用版本 3 需要一个作用域 PDU。
public static void main(String[] args) throws IOException {
String address = ("udp:192.168.60.105/161");
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
PDU pdu = new PDU();
pdu.setType(PDU.GET);
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.27675.20.5.2.0")));
Address targetAddress = GenericAddress.parse(address);
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
ResponseEvent response = snmp.send(pdu,target);
System.out.println(response);
System.out.println(response.getResponse());
System.out.println(response.getError());
}
运行上述代码的结果
org.snmp4j.event.ResponseEvent[source=org.snmp4j.Snmp@3f91beef]
null
null
我希望错误或响应都不为空。我正在使用 java 版本 java 版本“1.8.0_191”和 snmp4j 版本 2.5.0。代理正在运行 2.5.3
我使用wireshark跟踪了数据包,并且可以确认我从未使用snmp4j从代理那里得到响应。我对协议的了解不够,无法进行逐字节比较,但是net-snmp调用的信息列看起来与 snmp4j 调用非常不同。
网络-snmp
length info
106 get-request : sent from client to agent
159 report 1.3.6.1.6.3.15.1.1.4.0 : sent from agent to client
192 encryptedPDI : privKey unknown : sent from client to agent
196 encryptedPDI : privKey unknown : sent from agent to client
snmp4j - 响应从未收到从客户端到代理的三条消息
89 get-request 1.3.6.1.4.1.27675.20.5.2.0 sent from client to agent
查看字节的文本编码版本,我看到字符串“public”
解决方案
问题是代理是 SNMPv3,要求我使用带有一些授权信息的 ScopedPDU。用户和密码短语是从 ~/.snmp/snmp.conf 文件中获得的。我现在正在连接并得到代理的响应。代码如下所示。我没有得到正确的值,而是我发出了多少得到的计数器。但这是另一个问题。
第二个问题是 authProtocol 和 privProtocol 使用了错误的值。经验教训除了检查 errorResponse 之外,还要检查响应的 pdu 类型。响应类型的报告指示失败,报告的 OID 是导致失败的关键。
public static void main(String[] args) throws Exception {
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
OctetString localEngineId = new OctetString(MPv3.createLocalEngineID());
USM usm = new USM(SecurityProtocols.getInstance(), localEngineId, 0);
SecurityModels.getInstance().addSecurityModel(usm);
OctetString securityName = new OctetString("masked");
OID authProtocol = AuthMD5.ID;
OID privProtocol = PrivDES.ID;
OctetString authPassphrase = new OctetString("masked");
OctetString privPassphrase = new OctetString("masked");
snmp.getUSM().addUser(securityName, new UsmUser(securityName, authProtocol, authPassphrase, privProtocol, privPassphrase));
UserTarget target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(securityName);
target.setAddress(GenericAddress.parse(String.format("udp:%s/%s", "192.168.60.105", "161")));
target.setVersion(SnmpConstants.version3);
target.setRetries(2);
target.setTimeout(60000);
transport.listen();
PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.27675.20.10.1.2.0")));
pdu.setType(PDU.GET);
ResponseEvent event = snmp.send(pdu, target);
if (event != null) {
PDU pdu2 = event.getResponse();
System.out.println(pdu2.get(0).getVariable().toString());
if (pdu2.getErrorStatus() == PDU.noError) {
System.out.println("SNMPv3 GET Successful!");
} else {
System.out.println("SNMPv3 GET Unsuccessful.");
}
} else {
System.out.println("SNMP get unsuccessful.");
}
}
推荐阅读
- html -
在每个单词后带有换行符的块,宽度限制为最长的单词
- javascript - 选择下拉值时如何删除下拉列表的重复项
- javascript - 直接从firebase函数临时文件夹下载
- xcode - 禁用 Xcode 实用程序面板自动显示功能
- c# - 在 .net 核心中使用 Signalr 向单个用户和管理员角色用户发送通知
- shader - 在 WebGL 中排序实例
- flutter - 如何使用 Flutter 增加 Firebase 身份验证会话时间?
- amazon-web-services - VPC 中负载均衡器后面的私有 EC2 实例的路由
- r - 为什么我不能使用 write.xlsx 追加
- c++ - 删除原始数据后,Eigen Map 返回部分垃圾