Results 1 to 5 of 5
  1. #1
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default [SOLVED] Parsing window commands

    I have some code that simply uses the ping command inside windows (its not my code), i need to parse the average out of the code which appears on the end.

    However i've got no idea where to start? could anybody help out?

    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    
    public class NewClass3 {
    
    public static void main(String[] args) {
    
    String ip = "wwwdotgoogledotcom";
    String pingResult = "";
    
    String pingCmd = "ping " + ip;
    
    try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(pingCmd);
    
    BufferedReader in = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
    pingResult += inputLine;
    }
    in.close();
    
    }//try
    catch (IOException e) {
    System.out.println(e);
    }
    
    }
    
    }
    Last edited by VeasMKII; 01-30-2009 at 10:45 AM.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default not polite...

    Veas... you know, it not very nice to get code from one post and not even say thank you to the person who helped you get it. Now you're asking for more help, using that very same code. You seriously have got to learn how to be polite in a forum if you want help.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Yeah, it's not hard to say "Thanks..."

    First, you need to know what kinds of responses you can get. Basically, positive responses, no reply, can't find host, etc.

    Search for keywords in the response to determine what kinds of response you received. Look at String.indexOf().

    When you get a positive response, you want to break it into pieces, so you can get the data. Look as String.split(). I'm guessing

    String.split(" ")

    will do what you want. Know the position in the array of pieces of the data you want, and process that array entry.

  4. #4
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    I didn't mean to come off ungreatful, but it can seem that way without the whole story. My orginal post wasn't read properly, the information I had been directed to had been something i had found previously. I gave up programming on it for a while whilst busy with other things and now i'm attempting again.

    I certainly do appreciate the help, so thank you for that regardless.

    I've spent the last few hours toying with this and i've basically solved it, here is the bare bones code:

    Java Code:
    public void run() throws IOException {
        while(true){
        String pingCmd = "ping " + "wwwdotgoogledotcom"; //Send command "Ping + address"
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(pingCmd);
    
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inputLine;
                while ((inputLine = in.readLine()) != null){
                    System.out.println(inputLine); //DEBUG: Read out the data from process
                    if (inputLine.lastIndexOf("Average") == -1){    //If line contains "Average"
                        //do nothing when -1
                    }else{
                        //found Average @ line X
                        int i_avgPos = 
                        inputLine.lastIndexOf("Average") + 10;  //get end of average text
                        int i_msPos = 
                        inputLine.lastIndexOf("ms");            //get begining of ms
                        String s_latency = 
                        inputLine.substring
                        (i_avgPos, i_msPos);  //Return the characters between the two values (latency)
                        System.out.println("Average latency: " + s_latency);//DEBUG: show latency
                    }
                }
        }
    }
    The code looks for the last occurance of "Average" and "ms" finding their postions. Average is offset 10 bits to point to the first character of the latency information and ms is used to point to the last character. Then a substring is made using the characters between the values found, essentially extracting the average latency.

    I also had some code to check if disconnected, looks messy with that in too:
    Java Code:
    if(inputLine.lastIndexOf("Please check the name and try again.") > 0){//if disconnected
    This basically allows me to extract the latency (and detect the lack of a connection) using the windows ping utility. Which is basically everything i needed to create a latency indicator ;)
    Last edited by VeasMKII; 01-23-2009 at 04:49 PM.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Hey, I didn't mean to dump on you. That was more to support CJS. Answering questions on a forum can get annoying at times.

    Good for you. Your solution looks good. Don't forget to do something in the place where you have // Do nothing. It's bound to happen...

Similar Threads

  1. Forgotten Assembly Language Commands
    By Nicholas Jordan in forum Entertainment
    Replies: 1
    Last Post: 04-06-2010, 05:00 AM
  2. Execute the commands in Jsp Program
    By swetha_2008 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-17-2008, 06:04 AM
  3. Using Runtime to execute external commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:14 AM
  4. Windows Runtime Commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •