How to perform SNMP SET/GET using SNMP4J

SNMP4J – Free Open Source SNMP API for Java, enables java developers to write code to perform SNMP operations by using it’s api. There are many other api’s available to do this job, but SNMP4J is free to download and use, whereas other api most often comes with license that need to be purchased. SNMP4J can be downloaded from here.

SNMP Get and SNMP Set are two most commonly used functions among other SNMP operations available. Below is the sample code to use SNMP4J api to perform these two operations using Java.

SNMP SET Using SNMP4J


SNMP GET Using SNMP4J

Hope this information helped in writing your code a little quickly…!! Please leave your suggestions and feedback in the comments section below.

  1. Thank you for this tutorial, but i want to ask.,why the set method is not run well.,after i call set method, the value if mib is not changing. Thank you.

  2. Hi Regar,

    This is working absolutely fine for us. Please ensure that you are setting right values for – SNMP Version, host, community.

    Look at below line in given code:

    pdu.add(new VariableBinding(new OID(strOID), new Integer32(Value)));

    Ensure that you are setting correct MIB Attribute type, it may differ from Integer32.

    Another thing probably you can do is to add an else statement as below:

    if (strResponse!= null) {
    result = strResponse.getErrorStatusText();
    System.out.println(“Set Status is: “+result);
    } else {
    System.out.println(“Response is null”);
    }

    Let us know if we help you further on this.

  3. hi,
    thanks for this code,it was helpful for me,but I have problems when I tried to use the snmpget method,I always have an exception:
    java.lang.IllegalArgumentException: null/161
    this my code in the main class:

    public static void main(String[]args){
    SnmpTest client = new SnmpTest(); client.snmpGet(“127.0.0.1″,”public”,”1.3.6.1.2.1.1.1.0″);
    }

  4. Hi Yamato,

    There is an error in below line in snmpGet method.

    host= strAddress+”/”+”161″;

    Replace this with below line:

    host= host+”/”+”161″;

    We have updated the code in post. We regret the inconvenience caused. Let us know if it works for you now.

  5. hi,

    there is a problem in the set method as it always say no such a name.can you give an example how it work for JVM-MANAGEMENT-MIB if I want to set any value for example if I want to set the jvm thread count whose OID “1.3.6.1.4.1.42.2.145.3.163.1.1.3.1″ to 3 instead to 7. I try it but it say no such name

  6. Hi the kid,

    No such name error indicates that either the OID being used is not correct or community name is wrong. I did a quick search and found out that jvmThreadCount for which you shared the OID is read-only. [http://ipmsupport.solarwinds.com/mibs/JVM-MANAGEMENT-MIB/item.aspx?id=jvmThreadCount]. Hence it can not be set.

    But then, you should get readOnly error rather than noSuchName. I am not sure why are you getting noSuchName in this particular case.

    One thing which is confirmed is that you can’t set jvm thread count and it can only be read for monitoring purpose.

    You may refer below link to understand meaning of various snmp errors: http://verticalhorizons.in/snmp-errors-their-meaning-and-how-to-correct-them/

    Hope this helps. Let us know for further queries.

    • Hi Srinija,

      We are sorry but at present we do not have any C expert to answer to your query. But, once we have some expertise on it, we will get back to you.

      Regards

  7. Always, getting a null pointer exception at this place: if (response.getResponse().getErrorStatusText().equalsIgnoreCase(“Success”)) {

    i tried to print the value of response.getResponse() before condition if(response!=null){} condition. Got it as null…

    How should this be rectified in the Get method?

  8. Hello Admin,
    I have a suggestion please,why you can not develop an example of using the snmpwalk with SNMP4j API and thanks

  9. Hi Cyril,

    We believe you meant OID and not IOD. Set method only sets the value of a predefined OID and it can not create a new OID.

    Let us know if you are looking for further clarification.

    Regards

  10. Yes, my bad. I meant OID :)
    I’m looking way for create my own OID for check some feathers.

    How I can create a new OID for this purpose or maybe you have experience for this? Could you please suggest something regarding this issue?

    Best regards

  11. Hi Cyril,

    OIDs denote attributes of SNMP agent and they need to be pre-defined at agent level by MIB.

    Could you please let us know what exactly are you trying to do?

    Regards

  12. Hi!

    I’m trying to create a new gauge32 OID with predefined value or find existance gauge32 OID which I could change value. This OID should be use on Unix and Windows platfroms.
    I created shell script for Unix platforms and it work fine. But I could not understand how I can do this for Windows platforms.
    Because of that I would try do that on JAVA thru SNMP4J library.

    Regards

  13. hi,
    i am trying to run get method by following code
    public static void main(String[]args){
    SnmpTest client = new SnmpTest();
    client.snmpGet(“192.168.1.6″,”public”,”.1.3.6.1.2.1.1.1.0″);
    }
    but it is still not working and i am getting exception
    java.lang.NullPointerException
    at SnmpTest.snmpGet(SnmpTest.java:80)
    at SnmpTest.main(SnmpTest.java:101)
    and set function is always return a null response. kindly help me out with this.
    Regards

    • Hi Peerless,

      Are you still getting this exception. If yes, please paste the entire code here, as it seems that you have written a main method within the same class SnmpTest.java. It is difficult to trace the exception to the probable root cause.

      Regards

  14. Just to help anyone else out who happens upon this post, when I tried to add a Java main function (and class definition and all that) to the SNMP GET code I kept getting errors from the Java compiler, where it looks like the “str” variable was never initialized.

    I replaced this:
    if(strResponse.contains(“=”)) {
    int len = str.indexOf(“=”);
    strResponse=str.substring(len+1, str.length());
    }

    With this:
    if(strResponse.contains(“=”)) {
    String strNewResponse = null;
    int len = strResponse.indexOf(“=”);
    strNewResponse=strResponse.substring(len+1, strResponse.length());
    System.out.println(“The SNMP response to the OID requested is: ” + strNewResponse);
    }

    (I was getting no output when running it but I could see that an snmp get request and response was being generated, so I added my own System.out.println)

    Also my main function at the top looks like this:

    import org.snmp4j.CommunityTarget;
    import org.snmp4j.PDU;
    import org.snmp4j.Snmp;
    import org.snmp4j.TransportMapping;
    import org.snmp4j.event.ResponseEvent;
    import org.snmp4j.event.ResponseListener;
    import org.snmp4j.mp.SnmpConstants;
    import org.snmp4j.smi.*;
    import org.snmp4j.transport.DefaultUdpTransportMapping;

    public class snmpGet {
    public static void main(String[] args) {
    snmpGet client = new snmpGet();
    client.snmpGet(“192.168.1.8″,”public”,”1.3.6.1.2.1.1.1.0″);
    }

    I compile with this: javac -cp ~/java/snmp4j-2.1.0/src/ snmpGet.java

    And I run with this: java -cp ~/java/snmp4j-2.1.0/src:. snmpGet

    I tested this against a Linux VM that I installed snmpd on, and it works great! Thanks for the code… SNMP4J is a bit daunting, especially for someone new to Java like me.

  15. Hello , thank you for this tutorial , I want to ask a question about it :
    do I copy this code and run it ? or I have to write another class or code with it ? thank you

  16. Hi admin,

    What is host here?
    I’m getting a no such name responce when I try to do a GET from MIB browser after I set an OID using snmpSet()
    Please respond

  17. Hi admin,

    What is host here?
    I’m getting a no such name responce when I try to do a GET from MIB browser after I set an OID using snmpSet()
    Please respond

  18. Hey, You’ve done a fantastic job. I am going to unquestionably stumbleupon the idea and in my personal opinion highly recommend in order to friends and neighbors. I am sure will have them benefited from this excellent website.

  19. Hi,
    I have requirment like ,my webapplication get the alrets/erros informtion from my diffrent stores we are shown that details into goole maps but requirment is we want convert those errors into snmp traps and send to the host..
    could you provide any samples for this and iam new from this domain and fresher ….
    Regards
    Harirkishna

  20. when iam run above code following error is comming anybody help me.
    org.snmp4j.MessageException: Operation not permitted
    at org.snmp4j.MessageDispatcherImpl.sendPdu(MessageDispatcherImpl.java:526)
    at org.snmp4j.Snmp.sendMessage(Snmp.java:1004)
    at org.snmp4j.Snmp.send(Snmp.java:898)
    at org.snmp4j.Snmp.send(Snmp.java:878)
    at org.snmp4j.Snmp.send(Snmp.java:843)
    at org.snmp4j.Snmp.get(Snmp.java:555)
    at com.test.SnmpTest.snmpGet(SnmpTest.java:34)
    at com.test.SnmpTest.main(SnmpTest.java:94)
    Caused by: java.io.IOException: Operation not permitted
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:629)
    at org.snmp4j.transport.DefaultUdpTransportMapping.sendMessage(DefaultUdpTransportMapping.java:117)
    at org.snmp4j.transport.DefaultUdpTransportMapping.sendMessage(DefaultUdpTransportMapping.java:42)
    at org.snmp4j.MessageDispatcherImpl.sendMessage(MessageDispatcherImpl.java:202)
    at org.snmp4j.MessageDispatcherImpl.sendPdu(MessageDispatcherImpl.java:503)
    … 7 more

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">