2

I am trying to make a program in Java that gathers some information about the computer it's being executed on. I get the information by using things like:

 Process rt = Runtime.getRuntime ().exec("ipconfig");

In the exec("ipconfig") command, it returns a lot more information that I'm specifically asking for. For instance, I need the IP Address. In the command, it's labeled as "IPv4" (or something similar). I have used BufferedReader, to get the InputStream of the Process, and I converted the InputStream into String, and got the indexOf ("IPv4"). Unfortunately, that's as far as I can get. I need to convert it back into a String, that only contains the line that's labeled "IPv4".

I have found other questions on here, that are really similar to this, but I can't seem to understand them. Could someone please explain how to do this specifically, in detail?

Update: I need to do this from the Runtime method. I know that I could use the InetAddress methods, but this is just one example of it. I will be getting other information from the runtime, and I need to know how

user6615347
  • 101
  • 1
  • 7
  • 1
    If you got to the point that you have used `indexOf` and got it working successfully, then why not just use `String.substring()` and get what you want (given you know the syntax of what you're after)? – Idos Aug 26 '16 at 13:46
  • You may also be interested in looking at other ways to do this. Running this command will make your program work only on Windows I believe. How about something like one of these? http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet – ThePerson Aug 26 '16 at 13:49
  • You can reduce the output to your preferred one by using `grep`. –  Aug 26 '16 at 13:50
  • 1
    Speaking of other ways to do this, consider also getting the info from NetworkInterface without using an external command, gives you a more platform independent direct solution. See hints at http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java Doesn't answer your string parsing question, though. But its a good approach for the network info part at least. – Jason C Aug 26 '16 at 13:54

1 Answers1

2

Take a look at this Java pattern matching tutorial to get a basic understanding of how the Pattern and Matcher classes work. After that you should know enough to understand the following example:

public static String parseCommandOutput(String command, String regex, int group) {
    Pattern pattern = Pattern.compile(regex);
    Process process = Runtime.getRuntime().exec(command);
    try(InputStreamReader inputReader = new InputStreamReader(process.getInputStream());
        BufferedReader buffReader = new BufferedReader(inputReader))
    {
        String inLine = null;
        while( (inLine = buffReader.readLine()) != null ) {
            Matcher matcher = pattern.matcher(inLine);
            if(matcher.find()) {
                return inLine.substring(matcher.start(group), matcher.end(group));
            }
        }
        return null;
    }
}

For your particular problem, use the following input parameters:

String command = "ipconfig";
String regex = "(IPv4 Address.*: )(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
int group = 2;
String ipv4 = parseCommandOutput(command, regex, group); 
eighthrazz
  • 331
  • 1
  • 6