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;
|
}
|
|
|
}
|