kongdeqiang
2022-12-16 daf6a95086087ec99232eea8b4648b7541881f7c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.wgcloud.util;
 
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @version v3.3
 * @ClassName: PingUtil
 * @author: http://www.wgstart.com
 * @date: 2021年05月13日
 * @Description: 数通设备ping工具类
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
public class PingUtil {
 
    private static final Logger logger = LoggerFactory.getLogger(PingUtil.class);
 
 
    /**
     * ping ip返回在线或离线
     *
     * @param ipAddress ip地址
     * @param count     在默认情况下,一般都只发送四个数据包,通过这个命令可以自己定义发送的个数
     * @param timeOut   超时时间,秒
     * @return -1不在线,>-1的其他值在线(没有0的情况,最小是1)
     */
    public static long ping(String ipAddress, int count, 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 " + count + " -w " + (timeOut * 1000);
        } else {
            pingCommand = "ping " + " -c " + count + " -w " + timeOut + " " + ipAddress;
        }
        Integer diffTimes = StaticKeys.OUT_TIME_MARK;
        try {
            Process p = r.exec(pingCommand);
            if (p == null) {
                return StaticKeys.OUT_TIME_MARK;
            }
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                diffTimes = getCheckResult(line);
                if (diffTimes != StaticKeys.OUT_TIME_MARK) {
                    break;
                }
            }
        } catch (Exception ex) {
            logger.error("数通设备ping错误:", ex);
            return StaticKeys.OUT_TIME_MARK;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return diffTimes;
    }
 
    /**
     * 从结果中提取响应时间,先找整数,找不到再找带小数点
     *
     * @param line
     * @return
     */
    private static Integer getCheckResult(String line) {
        Integer times = StaticKeys.OUT_TIME_MARK;
        if (StringUtils.isEmpty(line)) {
            return times;
        }
        line = line.toLowerCase();
        if (line.contains("ttl=")) {
            line = line.replace(" ", "");
 
            //查找整数的正则 begin
            String regInt = "(=\\d+ms)";
            Pattern patternInt = Pattern.compile(regInt);
            Matcher matcherInt = patternInt.matcher(line);
            while (matcherInt.find()) {
                String groupStr = matcherInt.group(1);
                logger.debug(line + "--------------" + groupStr);
                groupStr = groupStr.replace("=", "").replace("ms", "");
                times = new Double(groupStr).intValue();
                //小于1ms,按照1ms处理
                if (times < 1) {
                    times = 1;
                }
                return times;
            }
            //查找整数的正则 begin
 
            //查找带小数点的正则 begin
            String regDouble = "(=\\d+.\\d+ms)";
            Pattern patternDouble = Pattern.compile(regDouble);
            Matcher matcherDouble = patternDouble.matcher(line);
            while (matcherDouble.find()) {
                String groupStr = matcherDouble.group(1);
                logger.debug(line + "--------------" + groupStr);
                groupStr = groupStr.replace("=", "").replace("ms", "");
                times = new Double(groupStr).intValue();
                //小于1ms,按照1ms处理
                if (times < 1) {
                    times = 1;
                }
                return times;
            }
            //查找带小数点的正则 end
 
            //没有匹配到 那应该是<1ms
            times = 1;
        }
        return times;
    }
 
 
}