peng
2026-03-24 73d124ce71e60685b19cc74a44e21dc080f7bfb6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.tievd.jyz.util;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
/**
 * @author zhangliang
 * @date 2022/9/5 17:01
 */
public class PingUtil {
    private static Logger logger = LoggerFactory.getLogger(PingUtil.class);
 
    /**ping  ipaddress 完整返回信息*/
    public static String executeLinuxCmd(String ipAddress, int pingTimes, int timeOut) {
        Runtime run = Runtime.getRuntime();
        String pingCommand;
        try {
            String osName = System.getProperty("os.name");
            if(osName.contains("Windows")){
                pingCommand = "ping " + ipAddress + " -n " + pingTimes    + " -w " + timeOut;
            }else{
                pingCommand = "ping " + " -c " + "4" + " -w " + "2 " + ipAddress;
            }
            Process process = run.exec(pingCommand);
            InputStream in = process.getInputStream();
            BufferedReader bs = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK")));
            StringBuffer out = new StringBuffer();
            String content = null;
            while ((content = bs.readLine()) != null) {
                out.append(content + "\n");
            }
            in.close();
            process.destroy();
            return out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**ping  ipaddress 完整返回true在线 false离线*/
    public static boolean ping(String ipAddress, int pingTimes, int timeOut) {
        BufferedReader in = null;
        String pingCommand;
        Runtime r = Runtime.getRuntime();
        String osName = System.getProperty("os.name");
        if(osName.contains("Windows")){
            pingCommand = "ping " + ipAddress + " -n " + pingTimes    + " -w " + timeOut;
        }else{
            pingCommand = "ping " + " -c " + "6" + " -w " + "10 " + ipAddress;
        }
        try {
            Process p = r.exec(pingCommand);
            if (p == null) {
                return false;
            }
            in = new BufferedReader(new InputStreamReader(p.getInputStream(),Charset.forName("GBK")));
            int connectedCount = 0;
            String line;
            while ((line = in.readLine()) != null) {
                connectedCount += getCheckResult(line,osName);
            }
            return connectedCount >= 1 ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace(); //出现异常则返回假
            return false;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static int getCheckResult(String line,String osName) {
        if(osName.contains("Windows")){
            if(line.contains("TTL=")){
                return 1;
            }
        }else{
            if(line.contains("ttl=")){
                return 1;
            }
        }
        return 0;
    }
 
 
}