I'm struggling from past few days to send SNMPV3 trap using Auth and priv phrase. The problem is : After i receive the first Trap SNMPv3 with Auth-Priv, i cannot receive the trap after sometimes (about 2minutes) . The receiver is like ignoring every trap which is coming.
Here is my Code :
The Receiver.java
try {
// set udpAdress and transportMapping
final UdpAddress udpAddress = new UdpAddress(162);
final TransportMapping transportMapping = new DefaultUdpTransportMapping(udpAddress);
// Protocole de sécurité +usm +snmp
this.snmp = new Snmp(new MessageDispatcherImpl(), transportMapping);
SecurityProtocols.getInstance().addDefaultProtocols();
final USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(MPv3.createLocalEngineID(new OctetString())),
0);
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());
usm.setEngineDiscoveryEnabled(true);
// Add the MPv
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(usm));
// Security model
SecurityModels.getInstance().addSecurityModel(usm);
// Add user
snmp.getUSM().addUser(new OctetString("v3AuthSHAPrivDESSecName"),
new UsmUser(new OctetString("v3AuthSHAPrivDESSecName"), AuthMD5.ID,
new OctetString("v3AuthSHAPrivDESAuthPassword"), PrivDES.ID,
new OctetString("v3AuthSHAPrivDESPrivPassword")));
// Launch of the listener
this.snmp.addCommandResponder(this);
snmp.listen();`
The Sender.java
try {
// set udpAdress and transportMapping
Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
// Protocole de sécurité +usm +snmp
SecurityProtocols.getInstance().addDefaultProtocols();
final USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(MPv3.createLocalEngineID(new OctetString())),
0);
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
// Ajout d'un user avec les paramètres de sécurité
snmp.getUSM().addUser(new OctetString("v3AuthSHAPrivDESSecName"),
new UsmUser(new OctetString("v3AuthSHAPrivDESSecName"), AuthMD5.ID,
new OctetString("v3AuthSHAPrivDESAuthPassword"), PrivDES.ID,
new OctetString("v3AuthSHAPrivDESPrivPassword")));
// Create Target
UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1111);
target.setTimeout(11111500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("v3AuthSHAPrivDESSecName"));
// Create PDU 1 for V3
ScopedPDU pdu = new ScopedPDU();
pdu.setType(PDU.TRAP);
pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
pdu.add(new VariableBinding(new OID(trapOid), new OctetString("V33333")));
snmp.send(pdu, target);
System.out.println("Sending Trap to (IP:Port)=> " + ipAddress + ":" + port);
snmp.addCommandResponder(new CommandResponder() {
public void processPdu(CommandResponderEvent arg0) {
System.out.println(arg0);
}
});
snmp.close();
I'm using the snmp4j-1.1 dependency. I don't know why it ignore the traps which come about 2minute after the first the send.
Many Thanks friends for your helps
I found the answer! :D Just change the dependency to snmp4j-2.* and it works, now i used the snmp4j-2.5.0. and it work perfect, the code above work perfect if you want to use it.
Have a nice code day :-)