多网IP探测-过滤无网关网卡
This commit is contained in:
@@ -251,6 +251,9 @@ public class AgentUtil {
|
|||||||
return swapped;
|
return swapped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 存储网关信息:接口名 -> 网关IP
|
||||||
|
private static Map<String, String> gatewayMap = new HashMap<>();
|
||||||
|
|
||||||
// 正则表达式匹配:当前 IP:xxx.xxx.xxx.xxx 来自于:中国 江苏省 南京市 电信
|
// 正则表达式匹配:当前 IP:xxx.xxx.xxx.xxx 来自于:中国 江苏省 南京市 电信
|
||||||
private static final String REGEX = "来自于:(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)";
|
private static final String REGEX = "来自于:(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)";
|
||||||
/**
|
/**
|
||||||
@@ -280,6 +283,12 @@ public class AgentUtil {
|
|||||||
String name = ni.getName();
|
String name = ni.getName();
|
||||||
if (!isEthernetInterface(name)) continue;
|
if (!isEthernetInterface(name)) continue;
|
||||||
|
|
||||||
|
// 检查是否“已连接”(物理链路状态)
|
||||||
|
if (!isInterfaceConnected(name)) continue;
|
||||||
|
|
||||||
|
// 检查IP4信息
|
||||||
|
if (getIPv4Address(ni).equals("N/A")) continue;
|
||||||
|
|
||||||
NetworkInterfaceInfo info = NetworkInterfaceInfo.builder()
|
NetworkInterfaceInfo info = NetworkInterfaceInfo.builder()
|
||||||
.name(name)
|
.name(name)
|
||||||
.type("Ethernet")
|
.type("Ethernet")
|
||||||
@@ -390,6 +399,42 @@ public class AgentUtil {
|
|||||||
return "N/A";
|
return "N/A";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查接口是否“已连接”(物理链路状态)
|
||||||
|
private static boolean isInterfaceConnected(String interfaceName) {
|
||||||
|
try {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
Process process;
|
||||||
|
|
||||||
|
if (os.contains("win")) {
|
||||||
|
// Windows: 使用 wmic 检查网卡是否启用
|
||||||
|
process = Runtime.getRuntime().exec(
|
||||||
|
"wmic nic where \"NetEnabled=true\" get Name");
|
||||||
|
} else {
|
||||||
|
// Linux: 检查 operstate
|
||||||
|
process = Runtime.getRuntime().exec(
|
||||||
|
"cat /sys/class/net/" + interfaceName + "/operstate");
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(process.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (os.contains("win")) {
|
||||||
|
if (line.trim().equalsIgnoreCase(interfaceName)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return "up".equals(line.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("无法检查接口状态: " + interfaceName);
|
||||||
|
return false; // 保守处理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取公网 IP
|
* 获取公网 IP
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user