Category Archives: Java

Performing SNMP walk and get operations using SNMP4j and Java

I was recently tasked with writing some java code to retrieve values, using SNMP and Java. Obviously, I turned to  the SNMP4j library, but to my dismay, the documentation was a little vague on how to simply achieve a GET and WALK operation, and I did not want to spend that much time writing code that was going to be used only once.

Thankfully Google provided some good examples, but as it happens often, none of them were quite complete, as to say, not exactly what I needed. So I am sharing my scripts that I used to achieve my objectives.

SNMP Get

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 SnmpGet() {
		// TODO Auto-generated constructor stub
	}


	/*
	 *  SnmpGet method

	 * return Response for given OID from the Device.

	 */

	public static String snmpGet(String strAddress, String community, String strOID, int snmpVersion)

	{

		String str="";

		try

		{

			OctetString community1 = new OctetString(community);

			strAddress= strAddress+"/" + 161;

			Address targetaddress = new UdpAddress(strAddress);

			TransportMapping transport = new DefaultUdpTransportMapping();

			transport.listen();

			CommunityTarget comtarget = new CommunityTarget();

			comtarget.setCommunity(community1);

			if(snmpVersion == 1){
				comtarget.setVersion(SnmpConstants.version1);
			}
			
			if(snmpVersion == 2){
				comtarget.setVersion(SnmpConstants.version2c);
			}
			
			if(snmpVersion == 3){
				comtarget.setVersion(SnmpConstants.version3);
			}
	

			comtarget.setAddress(targetaddress);

			comtarget.setRetries(2);

			comtarget.setTimeout(5000);

			PDU pdu = new PDU();

			ResponseEvent response;

			Snmp snmp;

			pdu.add(new VariableBinding(new OID(strOID)));

			pdu.setType(PDU.GET);

			snmp = new Snmp(transport);

			response = snmp.get(pdu,comtarget);

			if(response != null)

			{

				if(response.getResponse().getErrorStatusText().equalsIgnoreCase("Success"))

				{

					PDU pduresponse=response.getResponse();

					str=pduresponse.getVariableBindings().firstElement().toString();

					if(str.contains("="))

					{

						int len = str.indexOf("=");

						str=str.substring(len+1, str.length());

					}

				}

			}

			else

			{

				System.out.println("Feeling like a TimeOut occured ");

			}

			snmp.close();

		} catch(Exception e) { e.printStackTrace(); }

		System.out.println("Response="+str);

		return str;

	}
	
	
}

SNMP Walk

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TreeUtils;
import org.snmp4j.util.TreeEvent;
 
public class Snmpwalk {  
  private String targetAddr;
  private String oidStr;
  private String commStr;
  private int snmpVersion;
  private String portNum;  
  private String usage;
  private ArrayList<String> results = new ArrayList<String>();
    
  Snmpwalk() throws IOException{
    // Set default value.
    targetAddr = null;
    oidStr = null;
    commStr = "public";
    snmpVersion = SnmpConstants.version2c;
    portNum =  "161";
    usage = "Usage: snmpwalk [-c communityName -p portNumber -v snmpVersion(1 or 2)] targetAddr oid";
  }
  
  public Snmpwalk(HashMap<String, String> values){
      targetAddr = values.get("ipaddr");
	  oidStr = values.get("oid");
	  commStr = values.get("communityString");
	  int tempVersion = Integer.parseInt(values.get("version"));
	  if(tempVersion == 1){
		  snmpVersion = SnmpConstants.version1;
	  }
	  if(tempVersion == 2){
		  snmpVersion = SnmpConstants.version2c;
	  }
	  if(tempVersion == 3){
		  snmpVersion = SnmpConstants.version3;
	  }
	  portNum =  "161";
  }
  
  public void doSnmpwalk() throws IOException  {
    Address targetAddress = GenericAddress.parse("udp:"+ targetAddr + "/" + portNum);
    TransportMapping transport = new DefaultUdpTransportMapping();
    Snmp snmp = new Snmp(transport);
    transport.listen();
  
    // setting up target
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(commStr));
    target.setAddress(targetAddress);
    target.setRetries(3);
    target.setTimeout(1000 * 3);
    target.setVersion(snmpVersion);
  
    OID oid = null;
    try{
      oid = new OID(oidStr);
    }
    catch(RuntimeException ex){
      System.out.println("OID is not specified correctly.");
      System.exit(1);
    }
  
    TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());      
    List<TreeEvent> events = treeUtils.getSubtree(target, oid);
    if(events == null || events.size() == 0){
      System.out.println("No result returned.");
      System.exit(1);
    }
    
    // Get snmpwalk result.
    for (TreeEvent event : events) {
      if(event != null){
        if (event.isError()) {
            System.err.println("oid [" + oid + "] " + event.getErrorMessage());
          }
            
        VariableBinding[] varBindings = event.getVariableBindings();
        if(varBindings == null || varBindings.length == 0){
          System.out.println("No result returned.");
        }
        for (VariableBinding varBinding : varBindings) {
          results.add(varBinding.getVariable().toString());
        }
      }
    }
    
    //print results
    for(String value: results){
    	System.out.println(value);
    }
    snmp.close();
  }
  
  public boolean isInResults(String value){
	  return results.contains(value);
  }
    
  
  private void checkAndSetArgs(String[] args){
    if(args.length < 2){
      System.err.println(usage);
      System.exit(1);
    }
    
    for (int i=0; i<args.length; i++){
      if("-c".equals(args[i])){
        commStr = args[++i]; 
      }
      else if ("-v".equals(args[i])){
        if(Integer.parseInt(args[++i]) == 1){
          snmpVersion = SnmpConstants.version1;
        }
        else {
          snmpVersion = SnmpConstants.version2c;
        }
      }
      else if ("-p".equals(args[i])){
        portNum = args[++i];
      }
      else{
        targetAddr = args[i++];
        oidStr = args[i];
      }
    }
    if(targetAddr == null || oidStr == null){
      System.err.println(usage);
      System.exit(1);
    }
  }

The above code has been tested to work with SNMP version 1 and 2, but should work with 3 as well.

Hope this helps…!

Nostalgia – some university projects

While sifting through the files of my old system, I found a few of my university projects, which at the time, were a mess to handle. So I thought, “what the hell, share it with the world!”. So here are a few of them:

Mahjong Solitaire (Java)

This project involved creating an interactive Java based Májiàng Solitaire game, with multiple options of play, difficulty levels, different maps and different simple A.I solvers. We also did testing on the project on the different milestones of its development using JUnit testing.

Get the source code here.

Breakthrough (Java)

In this project I designed an artificially intelligent player to play the board game Breakthrough. The program was designed in Java using concepts of neural networks.

Get the source code here.

Disk Emulator(C)

Using C, developed a virtual disk emulator to perform all disk operations (read/write/data transfer).

Get the source code here.

Itinerary Reservation System (Java)

This Project involved creating a persistent travel itinerary reservation system, using distributed systems, both server side and client side programming involving JAVA, JGroups and RMI registry. There were three reservation options of cars, hotel rooms and flights, each handled on a separate server, with a persistent database file system to store all the data. Involved some socket programming concepts as well.

Get the source code here

Just a word of caution.These projects were made a while back, and I am not sure whether they completely work at the moment or not. I am just uploading them for recording reasons mostly and if someone wants to use them as a reference, since at the time I submitted them, they were working perfectly.

Have fun!

Project Euler – some thoughts

I found Project Euler a long while back, and it seemed to be a good way to practice some programming when I was bored. The website mostly offers mathematical problems, whose numerical solution needs to be calculated and has to be verified on the website. As you can see, I have only solved about 22 of the problems, but who has much time these days. I uploaded the programs I used for solving these problem to my git repo.

Although problem solving is the basic concept behind this website, I learnt quite early, that most of the problems that were given here needed efficient algorithms, otherwise they would take forever to spit out the answer. Not that I made the best algorithms, but the ones that I solved have decent enough times to give a solution within a minute at max.

I am not sure if i’ll ever be bored enough to start up with this again, but it was a nice experience going through some of the problems.