package com.changfa.utils;

import lombok.SneakyThrows;
import org.springframework.util.StringUtils;

import java.net.InetAddress;

public class PingUtils {

    private final static int DEFAULT_TIMEOUT = 5000;
    @SneakyThrows
    public static boolean startPing(String ipAddress) {
        if(!StringUtils.hasLength(ipAddress)){
            return false;
        }
        //超时应该在3钞以上
        try{
            boolean status = InetAddress.getByName(ipAddress).isReachable(DEFAULT_TIMEOUT);
            return status;
        }catch (Exception e){

        }
        return false;
    }

    @SneakyThrows
    public static boolean startPing(String ipAddress,int timeout) {
        if(!StringUtils.hasLength(ipAddress)){
            return false;
        }
        //超时应该在3钞以上
        try{
            boolean status = InetAddress.getByName(ipAddress).isReachable(timeout);
            return status;
        }catch (Exception e){

        }
        return false;
    }

    public static void main(String[] args){
        System.out.println(startPing("26.74.117.3"));
    }
}
