76638

Getting the ping results using java

Question:

I'm trying to get the last line of the ping result. I'm not a java developer. I'm learning so please bear my mistakes.

This is the program I have written.

private static String pingTest(String ip) { // TODO Auto-generated method stub String pingResult = ""; // System.out.println("Came in pingTest"); String pingCmd = "ping -c 3 " + 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(); } catch (IOException e) { System.out.println(e); } return pingResult;

I'm getting the result as follows:

<blockquote>

PING google.com (74.125.236.165) 56(84) bytes of data.64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=1 ttl=52 time=20.5 ms64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=2 ttl=52 time=11.4 ms64 bytes from maa03s16-in-f5.1e100.net (74.125.236.165): icmp_req=3 ttl=52 time=19.6 ms--- google.com ping statistics ---3 packets transmitted, 3 received, 0% packet loss, time 2002msrtt min/avg/max/mdev = 11.494/17.252/20.579/4.089 m

</blockquote>

Its ot properly formatted. how to add line breaks? Well. what I really want is something like this.

Only the last line.

rtt min/avg/max/mdev = 20.774/20.962/21.085/0.135 ms

and I want to show the values of min,avg,max values in my jsp page. Please guide me.

Answer1:

try

pingResult += inputLine + "\n";

note that it's typical to use StringBuilder in such situations, no matter what perfomance impact is

Answer2:

Update: I updated the code. Now I'm getting only last line as output.

private static String pingTest(String ip) { // TODO Auto-generated method stub String pingResult = ""; // System.out.println("Came in pingTest"); String pingCmd = "ping -c 3 " + 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); if (inputLine.contains("rtt")) pingResult += inputLine+ "\n"; } in.close(); } catch (IOException e) { System.out.println(e); } return pingResult; }

Thanks

Recommend

  • Parsing a file with single and multi-lines of data
  • Android: URL Connection accessing to website
  • AWS Lambda - Buffered reader
  • Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range
  • Joomla 3.2 Grouped List Custom Field List doesn't have SELECTED value
  • How do search engines recognize search boxes on websites?
  • Get process output without blocking
  • Is it possible to clear the console tab during runtime in Intellij with Java?
  • Restlet reuse InputStream
  • Having trouble with a simple JSON Hello World app
  • Open a file from the same package
  • How to read data from socket connection - android
  • C# where to add a method
  • Find the associated program to open a file using Java
  • vi mode to emacs mode while on R
  • Runtime error in UVA Online Judge [closed]
  • 3.0.0.M1: SSL - Invalid keystore format
  • Portable JRE on Linux - possible?
  • How to copy styled text in JTextPane
  • How to retrieve information from antrun back to maven?
  • Reading a file into a multidimensional array
  • How can I sort a a table with VBA with given text condition?
  • Converting a WriteableBitmap image ToArray in UWP
  • How to test if a URL from an Eclipse bundle is a directory?
  • How to install a .deb file on a jailbroken iphone programmatically?
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • javascript inside java/jsp code
  • Running a C# exe file
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • LevelDB C iterator
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • How do I use LINQ to get all the Items that have a particular SubItem?