Solved

create a java application for RedHat and run the DMIDecode to return the data

Posted on 2011-03-13
3
1,450 Views
Last Modified: 2013-12-14
public class SerNum {

Could someone tell me what is wrong with the code below? I'm trying to create a java application that will run in RedHat. I would like to get the serial number of the system by using DMIDecode and return it as a string.


   


 public static String GetSerNum() {

        String[] command = { "dmidecode -s system-serial-number" };

        String sNum = null;
 

        try
         {

       Process SerNumProcess = Runtime.getRuntime().exec(command);

      BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));

            sNum = sNumReader.readLine().trim();

            SerNumProcess.waitFor();

            sNumReader.close();

        }

        catch (Exception ex)
        {
           

            sNum = "Did not work";

        }

        finally
        {

            return sNum;

        }

    }
0
Comment
Question by:bojeff30
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35125442
whats the current problem with it?
0
 
LVL 4

Expert Comment

by:subratabiswas
ID: 35125685
Following is the same code that you provided, just did some formatting, added the necessary imports and the main function, and (importantly) declared the variable 'command' as a string.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SerNum {
    public static String GetSerNum() {
        String command = "dmidecode -s system-serial-number";
        String sNum = null;
        try {  
            Process SerNumProcess = Runtime.getRuntime().exec(command);
            BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
            sNum = sNumReader.readLine().trim();
            SerNumProcess.waitFor();
            sNumReader.close();
        } catch (Exception ex) {
            sNum = "Did not work";
        } finally {
            return sNum;
        }      
    }
    public static void main(String argv[]) {
        SerNum s = new SerNum();
        System.out.println(s.GetSerNum());
    }
}



See below the feedback from compilation and execution:

$ javac SerNum.java
$ export CLASSPATH=.
$ java SerNum
Did not work
$ dmidecode -s system-serial-number
/dev/mem: Permission denied

Same steps executed as root


# cd /home/subrata/temp
# export CLASSPATH=.
# java SerNum
5Y2BKLMZM1GM
# dmidecode -s system-serial-number
5Y2BKLMZM1GM

As seen in the above, there are many possible issues:

1) imports were missing; this would result into compilation error
2) main function was missing, this would also result in /missing main function' complaint
3) The original did not have execute privilege on the command dmidecode
4) Declared command as a simple string, not as an array of strings. The way it was provided, the whole string, including the intended arguments, were being considered as the executable, whlie naturfally there is no executable by that long name.

0
 
LVL 4

Accepted Solution

by:
subratabiswas earned 500 total points
ID: 35125864
To elaborate on 3) above, the original user that created the java program, compiled it and tried to execute it, did not have the necessary privilege to execute the underlying command dmidecode. On my test machine, this was a privileged command, so the command threw error message for non-privileged users. However, when the command was executed as root, it executed correctly, both when invoked from command prompt and indirectly from within  the java program.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now