SNMP4j-sned(pdu,target)方法总是返回null



我使用MIB浏览器引擎作为SNMP管理器,并从Java应用程序发送陷阱,如下所示场景1-(有效IP地址(场景2-(无效IP地址(在这两种情况下,作为响应为null

公共类TrapSenderVersion2{

public static void main(String[] args) {
TrapSenderVersion2 trapV2 = new TrapSenderVersion2();
trapV2.sendTrap_Version2();
}
public void sendTrap_Version2() {
try {
// Create Transport Mapping
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
// Create Target
CommunityTarget cTarget = new CommunityTarget();
cTarget.setCommunity(new OctetString("public"));
cTarget.setVersion(SnmpConstants.version1);
cTarget.setAddress(new UdpAddress("10.133.14.35/162"));
cTarget.setRetries(2);
cTarget.setTimeout(5000);
// Create PDU for V2
PDU pdu = new PDU();
// need to specify the system up time
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new OctetString(new Date().toString())));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(TrapOid)));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.6.0"), new OctetString("23b77493-74dd-489a-9c99-61db6c97a2e1"))); 
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.7.0"), new OctetString("EventType")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.8.0"), new OctetString("ServiceType")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.9.0"), new OctetString("inp44vpdl002-COLLECTOR")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.10.0"), new OctetString("14.140.156.15")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.11.0"), new OctetString("Collector"))); 
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.12.0"), new OctetString("Pune")));
pdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.30450.1.9.1.1.13.0"), new OctetString("This is a addtional info text")));

pdu.setType(PDU.NOTIFICATION);
// Send the PDU
Snmp snmp = new Snmp(transport);
System.out.println("Sending V2 Trap... Check Wheather NMS is Listening or not? ");
ResponseEvent re = snmp.send(pdu, cTarget);
System.out.println("ResponseEvent " + re);
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
}

下面的行解决了我的问题

pdu.setType(PDU.INFORM);

我通过调试snmp4j源代码得到了答案,它完全取决于PDU类型。当我们将PDU类型设置为Trap、Notificationm时,响应将始终返回为null。

所以有一种叫做"INFORM"的类型,它通过ResponseEvent来确认请求。

最新更新