Ping.java 847 B

1234567891011121314151617181920212223242526
  1. package com.its.op.utils;
  2. import java.io.IOException;
  3. import java.net.InetAddress;
  4. public class Ping {
  5. public static boolean isReachable(String host, int timeoutSec) throws IOException, InterruptedException {
  6. if (OS.isWindows())
  7. return Ping.winPing(host);
  8. return Ping.unixPing(host, timeoutSec);
  9. }
  10. private static boolean winPing(String host) throws IOException, InterruptedException {
  11. String cmd = "cmd /c ping -n 1 " + host + " | find \"TTL\"";
  12. Process proc = Runtime.getRuntime().exec(cmd);
  13. int exit = proc.waitFor();
  14. return (exit == 0) ? true : false;
  15. }
  16. private static boolean unixPing(String host, int timeoutSec) throws IOException {
  17. InetAddress targetIp = InetAddress.getByName(host);
  18. return targetIp.isReachable(timeoutSec*1000);
  19. }
  20. }