ptping

ptping is yet another try to integrate the ping command into Java. It uses the process API to execute the command and wrap the response in an object, essentially a wrapper around the ping command. ptping can be used both on Windows and on Unix environments.

Here’s the code.

You can get the complete output as a String as if running the command from the command line.

PingResult pingResult = PingCommandRunner.executePing("google.com");

System.out.println(pingResult.getCompleteOutput());

// Output:
// Pinging google.com [172.217.16.110] with 32 bytes of data:
// Reply from 172.217.16.110: bytes=32 time=14ms TTL=57
// Reply from 172.217.16.110: bytes=32 time=14ms TTL=57
// Reply from 172.217.16.110: bytes=32 time=24ms TTL=57
// Reply from 172.217.16.110: bytes=32 time=11ms TTL=57
// Reply from 172.217.16.110: bytes=32 time=13ms TTL=57
// Ping statistics for 172.217.16.110:
// Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
// Approximate round trip times in milli-seconds:
// Minimum = 11ms, Maximum = 24ms, Average = 15ms

And you can query for each segment of the response.

System.out.println(pingResult.isSuccessful());
// true

System.out.println(pingResult.getPingTarget());
// google.com

System.out.println(pingResult.getTargetIp());
// 172.217.18.78

System.out.println(pingResult.getPingBytes());
// 32

System.out.println(pingResult.getSentPackagesCount());
// 5

System.out.println(pingResult.getReceivedPackagesCount());
// 5

System.out.println(pingResult.getLostPackagesCount());
// 0

System.out.println(pingResult.getLostPackagesPercentage());
// 0.0

System.out.println(pingResult.getRoundTripMinimumTime());
// 11.0

System.out.println(pingResult.getRoundTripMaximumTime());
// 24.0

System.out.println(pingResult.getRoundTripAverageTime());
// 15.0

You can also query each ping attempt line.

pingResult.getAttemptLines().forEach(attemptLine ->
                System.out.println(attemptLine.getTime()));
// 14.0
// 14.0
// 24.0
// 11.0
// 13.0

pinging multiple targets at the same time is also possible.

List<PingResult> pingResults = PingCommandRunner
    .executePingToTargets("yahoo.com", "google.com");

PingResult objects are in the same order as target arguments.

For more info see the GitHub repo.