Discussion:
[SNMP4J] How to send synchronous SNMP GET response?
Ivan Mladenović
2010-04-07 08:20:11 UTC
Permalink
Hi to all,

I’m trying to implement SNMP GET operation. I want to send a synchronous
SNMP get request, and to process response.

I try something like this:

Address targetAddress = GenericAddress.*parse*("udp:127.0.0.1/161");



CommunityTarget target = *new* CommunityTarget();

target.setCommunity(*new* OctetString("public"));

target.setAddress(targetAddress);

target.setRetries(1);

target.setTimeout(10000);

target.setVersion(SnmpConstants.*version2c*);



// creating PDU

PDU pdu = *new* PDU();

pdu.add(*new* VariableBinding(new OID(new int[] {1,3,6,7}));

pdu.setType(PDU.*GET*);

pdu.setErrorIndex(0);

pdu.setErrorStatus(PDU.*noError*);

pdu.setMaxRepetitions(1);



TransportMapping transport = *new* DefaultUdpTransportMapping();

Snmp snmp = *new* Snmp(transport);



transport.listen();



ResponseEvent response = snmp.send(pdu, target);


I got some response (not null), but for response.getResponse() I got null.

When I try this from MIB browser, I get response with information that I
want to get.

I suppose here is something wrong.

Could you please give me some example or tell me what I’m doing wrong?

Thans

Ivan Mladenović
2010-04-08 09:50:59 UTC
Permalink
Hello,

Could somebody give me some example how to send GET request, by using
SNMP4J?

I try following things:

ResponseEvent response = snmp.send(pdu, target);

and

ResponseListener listener = *new* ResponseListener() {

* *@Override

* public* *void* onResponse(ResponseEvent event) {

* * PDU response = event.getResponse();

}

};



listener.onResponse(session.send(pdu, target));



Also, I try all examples from book “Essential SNMP, 2nd Edition“, and all
examples I was found on the internet.

But, every time I get same result. I get the response event, but response
PDU is always null. When I try this by MIB browser it is working properly.

It looks like when I send request my application does not wait to get
proper response.


Could anyone give me some example of how to send SNMP GET request by using
SNMP4J library?

Or, could anyone recommend some SNMP books about this?

Thanks in advance.



Ivan


2010/4/7 Ivan Mladenović <piffta at gmail.com>
I’m using specific OID (for example, 1.3.6.1.4.1.1.3.0). That OID is
defined in MIB file (used MIB file is specific for application).
I have implemented command responder that should send me response. This
working when I try to get value of this OID from MIB browser application.
2010/4/7 Rafał Krupiński <r.krupinski at gmail.com>
Post by Ivan Mladenović
pdu.add(*new* VariableBinding(new OID(new int[] {1,3,6,7}));
Is 1.3.6.7 a specific variable or some subtree? with GET you need to
provide OID of a variable, like 1.3.6.1.2.1.1.1.0
You could also use a sniffer, like wireshark and see what's sent over
the "wire".
--
Pozdrawiam / Best Regards
Rafal Krupinski
Ivan Mladenović
2010-04-09 11:49:53 UTC
Permalink
Hi, to all

Here is a complete example of how to send get request by using SNMPv2c and
SNMPv3.

* *

*SNMPv2c*

* *

Example of how to send SNMPv2c request contain two parts. First part
contains sending synchronous get request. Second part contains example how
to send get request by using custom command responder implementation.



*Sending SNMPv2c synchronous get request*

* *

TransportMapping transport = *new* DefaultUdpTransportMapping();



Snmp snmp = *new* Snmp(transport);



PDU requestPDU = *new* PDU();

requestPDU.add(*new* VariableBinding(*new* OID("1.3.6.1.2.1.1.1.0")));

requestPDU.setErrorIndex(0);

requestPDU.setErrorStatus(0);

requestPDU.setMaxRepetitions(0);

requestPDU.setType(PDU.*GET*);



Address targetAddress = *new* UdpAddress("127.0.0.1/161");



OctetString community = *new* OctetString("public");



CommunityTarget target = *new* CommunityTarget();

target.setAddress(targetAddress);

target.setCommunity(community);

target.setRetries(0);

target.setTimeout(1000);

target.setVersion(SnmpConstants.*version2c*);



ResponseEvent responseEvent = snmp.send(requestPDU, target);

*if* (responseEvent.getResponse() != *null*) {

// Processing response...

} *else* {

// Response timeout...

}


snmp.listen();



snmp.send(requestPDU, target);



transport.close();

snmp.close();**

* *

*Sending SNMPv2c asynchronous get request*

* *

TransportMapping transport = *new* DefaultUdpTransportMapping();



Snmp snmp = *new* Snmp(transport);



PDU requestPDU = *new* PDU();

requestPDU.add(*new* VariableBinding(*new* OID("1.3.6.1.2.1.1.1.0")));

requestPDU.setErrorIndex(0);

requestPDU.setErrorStatus(0);

requestPDU.setMaxRepetitions(0);

requestPDU.setType(PDU.*GET*);



Address targetAddress = *new* UdpAddress("127.0.0.1/161");



OctetString community = *new* OctetString("public");



CommunityTarget target = *new* CommunityTarget();

target.setAddress(targetAddress);

target.setCommunity(community);

target.setRetries(0);

target.setTimeout(1000);

target.setVersion(SnmpConstants.*version2c*);



CommandResponder commandResponder = *new* CommandResponder() {

@Override

* public* *void* processPdu(CommandResponderEvent event) {

* *// Code for processing request.

* *}

};



snmp.addCommandResponder(commandResponder);



snmp.listen();



snmp.send(requestPDU, target);



transport.close();

snmp.close();**

* *

*SNMPv3*

* *

OctetString userName = *new* OctetString("userName");

OctetString authPW = *new* OctetString("MD5_encoded_string");

OctetString privPW = *new* OctetString("DES_encoded_string");



TransportMapping transport = *new* DefaultUdpTransportMapping();



Snmp snmp = *new* Snmp(transport);



*byte*[] localEngineID = MPv3.*createLocalEngineID*();



SecurityProtocols securityProtocols = SecurityProtocols.*getInstance*();

securityProtocols.addAuthenticationProtocol(*new* AuthMD5());



// Check security level.

USM usm = *new* USM(securityProtocols, *new* OctetString(localEngineID), 0);

usm.removeAllUsers();

SecurityModels.*getInstance*().addSecurityModel(usm);

snmp.setLocalEngine(localEngineID, 0, 0);



// User is authenticated by user name, authentication and privacy passwords.

UsmUser usmUser = *new* UsmUser(userName, AuthMD5.*ID*, authPW, PrivDES.*ID*,
privPW);



usm.addUser(userName, *null*, usmUser);



snmp.getUSM().addUser(userName, usmUser);





ScopedPDU requestPDU = *new* ScopedPDU();

requestPDU.add(*new* VariableBinding(*new* OID("1.3.6.1.2.1.1.1.0")));

requestPDU.setErrorIndex(0);

requestPDU.setErrorStatus(0);

requestPDU.setMaxRepetitions(1);

requestPDU.setType(PDU.*GET*);



Address targetAddress = *new* UdpAddress("127.0.0.1/161");



UserTarget target = *new* UserTarget();

target.setAddress(targetAddress);

target.setRetries(1);

target.setSecurityLevel(SecurityLevel.*AUTH_PRIV*);

target.setSecurityModel(SecurityModel.*SECURITY_MODEL_USM*);

target.setTimeout(1000);

target.setVersion(SnmpConstants.*version3*);



snmp.listen();



ResponseEvent responseEvent = snmp.send(requestPDU, target);



PDU responsePDU = responseEvent.getResponse();

*if* (responsePDU != *null*) {

// Processing response...

} *else* {

// Response timeout...

}



transport.close();

snmp.close();**

Loading...