package com.webclip.base;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * **********************
 *
 * @Author bug machine
 * 创建时间： 2026/1/16 15:30
 * 用途
 * **********************
 */
public class PingUtils {

    /**
     * 执行 ping 命令
     * @param address 域名或IP
     * @return 返回执行结果
     */
    public static String ping(String address) {
        StringBuilder result = new StringBuilder();
        try {
            // -c 4 表示执行4次
            Process process = Runtime.getRuntime().exec("ping -c 3 " + address);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }
            reader.close();
            process.destroy();
        } catch (Exception e) {
            result.append("Ping失败: ").append(e.getMessage());
        }
        return result.toString();
    }
}
