How to run the ping command and get the total number of ping hosts?

Hey. I need to execute the PING command using Java code and get the ping host summary. How to do it in Java?

+4
source share
3 answers

as stated in viralpatel, you can use Runtime.exec()

Below is an example

 class pingTest { public static void main(String[] args) { String ip = "127.0.0.1"; 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(); } catch (IOException e) { System.out.println(e); } } } 

Output

 Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms 

send http://www.velocityreviews.com/forums/t146589-ping-class-java.html

+10
source

InetAddress class has a method that uses ECCH Echo Request (aka ping) to determine host availability.

 String ipAddress = "192.168.1.10"; InetAddress inet = InetAddress.getByName(ipAddress); boolean reachable = inet.isReachable(5000); 

If the reachable variable above is true, this means that the host responded correctly with ECMP Echo Reply (aka pong) within the specified time (in milliseconds).

Note. Not all implementations should use ping. The documentation states that :

A typical implementation will use ICMP ECHO requests if privilege can be obtained, otherwise it will try to establish a TCP connection to port 7 (Echo) of the destination host.

Therefore, this method can be used to check host availability, but it cannot be universal for checking ping-based checks.

+3
source

Check out this ping library for java that I create:

http://code.google.com/p/jpingy/

Maybe this will help

+1
source

Source: https://habr.com/ru/post/1390369/


All Articles