Passing the file data into function

117 views Asked by At

Hi I have been trying to pass the ipAdresses which are in a file to a SNMP function which will perform a GET Operation. I am reading the file line by line and passing the data. But I am getting an error at setAddress, the program's works fine if I don't pass from the file. See in the code 1st line have commented ipAddress;

Code:

package com.snmp.discovery;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class Discover1 {

    private static String  port    = "8001";

    // OID of MIB CISCO-MGMT-MDM; Scalar Object = .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0
    private static String  oidValue  = ".1.3.6.1.2.1.1.5.0";  // ends with 0 for scalar object

    private static int    snmpVersion  = SnmpConstants.version1;

    private static String  community  = "public";
    public static void main(String[] args) {
         try {
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("textfile.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
                Discover1.getDevice(strLine);
            }
            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
    public static void getDevice(String a) throws Exception {
        System.out.println("SNMP GET Demo");
        System.out.println(a);
        // Create TransportMapping and Listen
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();

        // Create Target Address object
        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString(community));
        comtarget.setVersion(snmpVersion);

        comtarget.setAddress(new UdpAddress(a+"/"+port));
        System.out.println("------------");
        comtarget.setRetries(2);
        comtarget.setTimeout(1000);

        // Create the PDU object
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(oidValue)));
        pdu.setType(PDU.GET);
        pdu.setRequestID(new Integer32(1));

        // Create Snmp object for sending data to Agent
        Snmp snmp = new Snmp(transport);

        System.out.println("Sending Request to Agent...");
        ResponseEvent response = snmp.get(pdu, comtarget);

        if (response != null){
            System.out.println("Got Response from Agent");
            PDU responsePDU = response.getResponse();

            if (responsePDU != null){
                int errorStatus = responsePDU.getErrorStatus();
                int errorIndex = responsePDU.getErrorIndex();
                String errorStatusText = responsePDU.getErrorStatusText();

                if (errorStatus == PDU.noError){
                    System.out.println("Snmp Get Response = " + responsePDU.getVariableBindings());
                } else {
                    System.out.println("Error: Request Failed");
                    System.out.println("Error Status = " + errorStatus);
                    System.out.println("Error Index = " + errorIndex);
                    System.out.println("Error Status Text = " + errorStatusText);
                }
            } else {
                System.out.println("Error: Response PDU is null");
            }
        } else {
            System.out.println("Error: Agent Timeout... ");
        }
        snmp.close();
    }
}
2

There are 2 answers

0
Ambrish On BEST ANSWER

From the error message it looks like that you have spaces in the file. When you pass the IP address directly there are no spaces and hence it works. You need to remove the spaces before using it.

Change following line

Discover1.getDevice(strLine);

Remove leading and trailing blank spaces. Like:

Option 1:

Discover1.getDevice(strLine.trim());

Option 2:

Discover1.getDevice(StringUtils.trim(strLine));

Reference: StringUtils

2
Ron Thompson On

So this may not exactly be an answer, but it won't fit in the comments.

This is bad:

catch(Exception e) { .... }

ONLY do this if someone ELSE's method throws Exception. Also, punch that person if you can.

Your own method should NOT methodName() throws Exception. It should specifically methodName () throws IOException etc.

You should be doing catch(IOException e) { ... } catch(IllegalArgumentException e) {...} etc

As you've seen, it makes it hard to troubleshoot if you don't know what kind of exception is thrown.

Finally, when asking for help with an Exception, you should generally include the stack trace. For example:

Exception in thread "main" java.lang.NullPointerException at com.example.myproject.Book.getTitle(Book.java:16) at com.example.myproject.Author.getBookTitles(Author.java:25) at com.example.myproject.Bootstrap.main(Bootstrap.java:14)

This information is helpful with debugging.