Results 1 to 5 of 5
Thread: [SOLVED] Parsing window commands
- 01-23-2009, 11:36 AM #1
[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.
- 01-23-2009, 12:39 PM #2
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.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-23-2009, 04:19 PM #3
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.
- 01-23-2009, 04:45 PM #4
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:
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.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 } } } }
I also had some code to check if disconnected, looks messy with that in too:
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 ;)Java Code:if(inputLine.lastIndexOf("Please check the name and try again.") > 0){//if disconnectedLast edited by VeasMKII; 01-23-2009 at 04:49 PM.
- 01-23-2009, 05:22 PM #5
Similar Threads
-
Forgotten Assembly Language Commands
By Nicholas Jordan in forum EntertainmentReplies: 1Last Post: 04-06-2010, 05:00 AM -
Execute the commands in Jsp Program
By swetha_2008 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-17-2008, 06:04 AM -
Using Runtime to execute external commands
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:14 AM -
Windows Runtime Commands
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks