优化agent客户端网关探测
This commit is contained in:
@@ -608,7 +608,7 @@ public class AgentUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取默认网关(调用 shell 命令)
|
* 获取默认网关(调用 shell 命令)
|
||||||
*/
|
*/
|
||||||
private static String getGatewayAddress() {
|
public static String getGatewayAddress() {
|
||||||
try {
|
try {
|
||||||
Process process = Runtime.getRuntime().exec("ip route");
|
Process process = Runtime.getRuntime().exec("ip route");
|
||||||
java.util.Scanner scanner = new java.util.Scanner(process.getInputStream());
|
java.util.Scanner scanner = new java.util.Scanner(process.getInputStream());
|
||||||
@@ -625,6 +625,36 @@ public class AgentUtil {
|
|||||||
}
|
}
|
||||||
return "N/A";
|
return "N/A";
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 获取默认网关(调用 shell 命令)
|
||||||
|
*/
|
||||||
|
public static String getGatewayAddress(String interfaceName) {
|
||||||
|
try {
|
||||||
|
Process process = Runtime.getRuntime().exec("ip route");
|
||||||
|
java.util.Scanner scanner = new java.util.Scanner(process.getInputStream());
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
if (line.startsWith("default")) {
|
||||||
|
String[] parts = line.split(" ");
|
||||||
|
if (parts.length >= 5 && "via".equals(parts[1])) {
|
||||||
|
// 基本格式检查通过
|
||||||
|
for (int i = 0; i < parts.length; i++) {
|
||||||
|
if ("dev".equals(parts[i]) && i + 1 < parts.length) {
|
||||||
|
if (interfaceName.equals(parts[i + 1])) {
|
||||||
|
return parts[2]; // gateway地址
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
public static String getGateway(String interfaceName, String target) {
|
public static String getGateway(String interfaceName, String target) {
|
||||||
try {
|
try {
|
||||||
Process process = Runtime.getRuntime().exec(
|
Process process = Runtime.getRuntime().exec(
|
||||||
|
|||||||
@@ -93,12 +93,17 @@ public class NetworkInterfaceUtil {
|
|||||||
|
|
||||||
// 获取网关
|
// 获取网关
|
||||||
String gateway = AgentUtil.getGateway(name, "www.baidu.com");
|
String gateway = AgentUtil.getGateway(name, "www.baidu.com");
|
||||||
AssertLog.info(" 网关: {}", gateway);
|
if(gateway == null) {
|
||||||
|
// 降级
|
||||||
if ("N/A".equals(gateway)) {
|
gateway = AgentUtil.getGatewayAddress(name);
|
||||||
AssertLog.info(" 跳过网卡 {}: 无法获取网关", name);
|
AssertLog.info("traceroute探测失败,改为ip route方式获取");
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
AssertLog.info("获取到网关为: {}", gateway);
|
||||||
|
|
||||||
|
// if ("N/A".equals(gateway)) {
|
||||||
|
// AssertLog.info(" 跳过网卡 {}: 无法获取网关", name);
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
if(!"N/A".equals(ipv4) && gateway != null){
|
if(!"N/A".equals(ipv4) && gateway != null){
|
||||||
flag = true;
|
flag = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user