[SNMP4J] Broadcasting via SNMPv3 doesn't work properly
Vm
vijaymajagaonkar at gmail.com
Mon Jan 19 13:20:35 CET 2009
hope this may help you
[CODE]
package com.s7.admintools;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.smi.OID;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.PDU;
import org.snmp4j.event.ResponseListener;
public class SNMPBroadcast {
Vector<String> broadcastList = new Vector<String>();
/**
* @cons
*/
public SNMPBroadcast() {
}
/**
* This method is to broadcast SNMP Request and collect the list of
ipAddress
* @return broadcastList
*/
public Vector<String> broadCast() {
try {
Snmp snmp = AdminToolsCommon.createSnmp();
snmp.listen();
CommunityTarget target = AdminToolsCommon
.createNewTarget(AdminToolsCommon
.getBroadcastTargetAddress());
PDU command = AdminToolsCommon.createNewPDU();
command.add(new VariableBinding(new OID(new int[] { 1, 3, 6,
1, 2,
1, 1, 1, 0 })));
snmp.send(command, target, null, new ResponseListener() {
public void onResponse(ResponseEvent event) {
String[] ipAddress;
// only if machine is windows
if
(event.getResponse().toString().contains("Windows")) {
ipAddress = event.getPeerAddress().toString()
.split("/");
broadcastList.add(ipAddress[0]);
ipAddress = null;
}
}
});
Thread.sleep(1000);
} catch (Exception e) {
// TODO:
} finally {
}
return broadcastList;
}
} // End of Broadcast
[/CODE]
Dmitriy Dubovoy wrote:
>
>
> Hello,
>
>
>
> Please help me,
>
>
>
> I tried to send broadcast snmp request using SNMPv3 but always I received
> only one response.
>
> Please look at the my code below:
>
>
>
> try {
>
> ThreadPool tp = ThreadPool.create("Asynchronous SNMP Adaptor
> Thread", 3);
>
> MultiThreadedMessageDispatcher mtmd = new
> MultiThreadedMessageDispatcher(tp, new MessageDispatcherImpl());
>
> mtmd.addMessageProcessingModel(new MPv1());
>
> mtmd.addMessageProcessingModel(new MPv2c());
>
> mtmd.addMessageProcessingModel(new MPv3());
>
> mtmd.addCommandResponder(this);
>
>
>
> DefaultUdpTransportMapping defaultTransport = new
> DefaultUdpTransportMapping();
>
> defaultTransport.setReceiveBufferSize(65535);
>
> TransportMapping transport = defaultTransport;
>
> mtmd.addTransportMapping(transport);
>
>
>
> USM usm = new USM(SecurityProtocols.getInstance(),
>
> new OctetString(MPv3.createLocalEngineID()), 0);
>
> SecurityModels.getInstance().addSecurityModel(usm);
>
>
>
> OctetString securityName = new OctetString("admin");
>
>
>
> UsmUser usmUser = new UsmUser(securityName,
>
> AuthMD5.ID,
>
> new OctetString("adminadmin"),
>
> PrivDES.ID,
>
> new OctetString("adminadmin"));
>
>
>
> snmp.getUSM().addUser( securityName, usmUser);
>
>
>
> transport.addTransportListener(mtmd); // addMessageDispatcher()
> was deprecated in 1.7.4a.
>
>
>
> transport.listen();
>
>
>
> // create the PDU
>
> ScopedPDU pduScoped = new ScopedPDU();
>
>
>
> //Fill scoped pdu parameters
>
> logger.debug("asyncsend_v3(): Fill scoped pdu parameters ");
>
> pduScoped.setType(PDU.GET);
>
> pduScoped.add(new VariableBinding(new
> OID("1.3.6.1.2.1.2.2.1.10"))); //for example
>
>
>
> //set SecurityLevel
>
> int securityLevel = snmpTarget.getSecurityLevel();
>
>
>
> Address targetAddress = GenericAddress.parse("10.10.255.255/161");
>
> PduHandle reqHandle = mtmd.sendPdu(null, targetAddress,
> SnmpConstants.version3,
>
> SecurityModel.SECURITY_MODEL_USM,
> securityName.getValue(),
>
> SecurityLevel.NOAUTH_NOPRIV, pduScoped, true);
>
> } catch (IOException e) {
>
> logger.error("asyncsend(): Error sending async message. " + e);
>
> throw e;
>
> } catch (Exception e){
>
> logger.error("asyncsend(): Error sending async message. " + e);
>
> }
>
>
>
> ...
>
>
>
> public void processPdu(CommandResponderEvent e) {
>
> PDU responsePdu = e.getPDU();
>
> if (responsePdu == null)
>
> return;
>
>
>
> /*
>
> if (responsePdu.getType() != PDU.RESPONSE)
>
> return;
>
>
>
> Integer32 int32 =
> responsePdu.getRequestID();
>
> if (int32.getValue() == 0)
>
> return;
>
> */
>
>
>
>
>
> PduHandle pduHandle = e.getPduHandle();
>
> e.getPeerAddress();
>
>
>
> } catch (Exception exception) {
>
> }
>
> }
>
>
>
> After executing the first part of code I receives only one
> CommandResponderEvent.
>
> The same code for SNMP v1 works fine, I receives responses from all my
> devices:
>
>
>
> ThreadPool tp = ThreadPool.create("Asynchronous SNMP Adaptor Thread", 3);
>
> MultiThreadedMessageDispatcher mtmd = new
> MultiThreadedMessageDispatcher(tp, new MessageDispatcherImpl());
>
> mtmd.addMessageProcessingModel(new MPv1());
>
> mtmd.addMessageProcessingModel(new MPv2c());
>
> mtmd.addMessageProcessingModel(new MPv3());
>
> mtmd.addCommandResponder(this);
>
>
>
> DefaultUdpTransportMapping defaultTransport = new
> DefaultUdpTransportMapping();
>
> defaultTransport.setReceiveBufferSize(65535);
>
> TransportMapping transport = defaultTransport;
>
> mtmd.addTransportMapping(transport);
>
> // transport.addMessageDispatcher(mtmd);
>
> transport.addTransportListener(mtmd);
>
>
>
> transport.listen();
>
>
>
> Address targetAddress = GenericAddress.parse("10.10.255.255/161");
>
>
>
> PduHandle reqHandle = mtmd.sendPdu(null,
> targetAddress, SnmpConstants.version1,
>
>
> SecurityModel.SECURITY_MODEL_SNMPv1, new OctetString("public") .getValue(),
>
>
> SecurityLevel.NOAUTH_NOPRIV, pdu, true);
>
>
>
>
>
> All devices supports SNMPv3 and set up by the same settings.
>
> Could anyone help me to receive all responses? What I do incorrectly?
>
>
>
> Please help me!
>
>
>
> Thanks,
>
> Dmitriy
>
>
>
>
>
>
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
>
>
More information about the SNMP4J
mailing list