为什么使用SNMP4J而不是Net-SNMP时,查询中的最后一个索引会缺少?
这是snmp4j结果图像 当我使用Net-SNMP时,我可以获取总数据,但是当我使用SNMP4J时,我发现丢失了最后一个数据。 它仅返回8193.和getNext 8193 responsevent.getResponse()为null。什么是原因? NET -SNMP命令是:SNMPWALK -V 2C -C'密码'IP 1.3.6.1.2.1.31.1.1.1.1.7 这是net-snmp结果 这是SNMP4J代码:
Snmp snmp = SnmpComunication.getSession(snmpTarget.getSnmpVersion());
Target target = SnmpComunication.getSnmpTarget(snmp, snmpTarget, false);
Map<String, String> resultMap = new HashMap<String, String>();
try {
PDU pdu = null;
if (snmpTarget.getSnmpVersion() == MessageProcessingModel.MPv3) {
pdu = new ScopedPDU();
} else {
pdu = new PDU();
}
pdu.setType(PDU.GETNEXT);
OID targetOId = new OID(oid);
pdu.add(new VariableBinding(targetOId));
boolean matched = true;
while (matched) {
ResponseEvent responseEvent = snmp.send(pdu, target);
if (responseEvent == null || responseEvent.getResponse() == null) {
log.warn(“result is null!responseEvent:{},pdu:{}”,
JsonUtil.obj2Json(responseEvent),
JsonUtil.obj2Json(pdu.getVariableBindings()));
break;
}
PDU response = responseEvent.getResponse();
String nextOid = null;
List<? extends VariableBinding> variableBindings = response.getVariableBindings();
for (int i = 0; i < variableBindings.size(); i++) {
VariableBinding variableBinding = variableBindings.get(i);
nextOid = variableBinding.getOid().toDottedString();
if (nextOid.length() > oid.length() && resultMap.containsKey(nextOid.substring(oid.length() + 1))) {
log.info(“matched false! oid:{} have same subOid:{}”, oid, nextOid);
matched = false;
break;
}
if (!nextOid.startsWith(oid)) {
log.warn(“matched false!nextOid:{},oid:{}”,nextOid,oid);
matched = false;
break;
} else {
String varString = variableBinding.getVariable().toString();
log.info(“current oid:{},value:{}”,nextOid.substring(oid.length() + 1),varString);
resultMap.put(nextOid.substring(oid.length() + 1), varString);
}
}
if (!matched) {
break;
}
pdu.clear();
pdu.add(new VariableBinding(new OID(nextOid)));
log.info(“next oid:{}”,nextOid);
}
return resultMap;
} catch (Exception e) {
}
return null;
Here is the snmp4j result image
When I use NET-SNMP, I can get the total data,but when I use snmp4j,I find that last data is missing.
It only return 8193.and getnext 8193 the responseEvent.getResponse() is null . What is the reason?
The NET-SNMP command is: snmpwalk -v 2c -c 'password' ip 1.3.6.1.2.1.31.1.1.1.7
Here is the NET-SNMP result
Here is the snmp4j code :
Snmp snmp = SnmpComunication.getSession(snmpTarget.getSnmpVersion());
Target target = SnmpComunication.getSnmpTarget(snmp, snmpTarget, false);
Map<String, String> resultMap = new HashMap<String, String>();
try {
PDU pdu = null;
if (snmpTarget.getSnmpVersion() == MessageProcessingModel.MPv3) {
pdu = new ScopedPDU();
} else {
pdu = new PDU();
}
pdu.setType(PDU.GETNEXT);
OID targetOId = new OID(oid);
pdu.add(new VariableBinding(targetOId));
boolean matched = true;
while (matched) {
ResponseEvent responseEvent = snmp.send(pdu, target);
if (responseEvent == null || responseEvent.getResponse() == null) {
log.warn(“result is null!responseEvent:{},pdu:{}”,
JsonUtil.obj2Json(responseEvent),
JsonUtil.obj2Json(pdu.getVariableBindings()));
break;
}
PDU response = responseEvent.getResponse();
String nextOid = null;
List<? extends VariableBinding> variableBindings = response.getVariableBindings();
for (int i = 0; i < variableBindings.size(); i++) {
VariableBinding variableBinding = variableBindings.get(i);
nextOid = variableBinding.getOid().toDottedString();
if (nextOid.length() > oid.length() && resultMap.containsKey(nextOid.substring(oid.length() + 1))) {
log.info(“matched false! oid:{} have same subOid:{}”, oid, nextOid);
matched = false;
break;
}
if (!nextOid.startsWith(oid)) {
log.warn(“matched false!nextOid:{},oid:{}”,nextOid,oid);
matched = false;
break;
} else {
String varString = variableBinding.getVariable().toString();
log.info(“current oid:{},value:{}”,nextOid.substring(oid.length() + 1),varString);
resultMap.put(nextOid.substring(oid.length() + 1), varString);
}
}
if (!matched) {
break;
}
pdu.clear();
pdu.add(new VariableBinding(new OID(nextOid)));
log.info(“next oid:{}”,nextOid);
}
return resultMap;
} catch (Exception e) {
}
return null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论