过去几天我一直在努力使用 Auth 和 priv 短语发送 SNMPV3 陷阱。这是我的代码。
陷阱接收器
private void init() throws UnknownHostException, IOException {
threadPool = ThreadPool.create("Trap", 10);
dispatcher = new MultiThreadedMessageDispatcher(threadPool,
new MessageDispatcherImpl());
listenAddress = GenericAddress.parse(System.getProperty(
"snmp4j.listenAddress", "udp:0.0.0.0/165"));
TransportMapping<?> transport;
if (listenAddress instanceof UdpAddress) {
transport = new DefaultUdpTransportMapping(
(UdpAddress) listenAddress);
} else {
transport = new DefaultTcpTransportMapping(
(TcpAddress) listenAddress);
}
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
usm.setEngineDiscoveryEnabled(true);
snmp = new Snmp(dispatcher, transport);
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(usm));
SecurityModels.getInstance().addSecurityModel(usm);
snmp.getUSM().addUser( new OctetString("MD5DES"),
new UsmUser(new OctetString("karthikprasad"), AuthMD5.ID,
new OctetString("UserName"), PrivDES.ID,
new OctetString("PasswordUser")));
snmp.getUSM().addUser(new OctetString("MD5DES"),
new UsmUser(new OctetString("MD5DES"), null, null, null, null));
snmp.listen();
}
陷阱发送者
private static void sendSnmpV3Trap() {
try {
long start = System.currentTimeMillis();
Address targetAddress = GenericAddress.parse("udp:" + ipAddress
+ "/" + port);
// Create Transport Mapping
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser( new OctetString("MD5DES"),
new UsmUser(new OctetString("karthikprasad"), AuthMD5.ID,
new OctetString("UserName"), PrivDES.ID,
new OctetString("PasswordUser")));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
// set timeout
target.setTimeout(11500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("karthikprasad"));
target.setSecurityModel(SecurityModel.SECURITY_MODEL_USM);
// Create PDU for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(ScopedPDU.NOTIFICATION);
// need to specify the system up time
long sysUpTime = (System.currentTimeMillis() - start) / 10;
pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(
sysUpTime)));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID,
SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.1.1"),
new Integer32(1)));
// Send the PDU
System.out.println("Sending V3 Trap to " + ipAddress + " on Port "
+ port);
snmp.send(pdu, target);
snmp.addCommandResponder(new CommandResponder() {
@Override
public void processPdu(CommandResponderEvent arg0) {
System.out.println(arg0);
}
});
snmp.close();
} catch (Exception e) {
System.err.println("Error in Sending V2 Trap to " + ipAddress
+ " on Port " + port);
System.err.println("Exception Message = " + e.getMessage());
}
}
当我设置 noauthand nopriv 它工作正常。但是当我设置为 authpriv 时,我没有收到陷阱消息。当我启用调试时,甚至没有在发件人中收到任何错误。我尝试将文件复制到另一台机器并运行接收器并使用wireshark发送陷阱和监控网络,我能够找到到达目标服务器的消息,但它没有传递给接收器。我相信解密时发生了一些问题。有人可以帮帮我吗?顺便说一句,我正在使用 jdk 6 update 32。