sn获取方式改变,ipv4临时路由添加异常处理完善

This commit is contained in:
gaoyutao
2025-12-01 11:03:56 +08:00
parent 620c97ee5b
commit 1b3ec66803
2 changed files with 72 additions and 5 deletions
@@ -298,7 +298,7 @@ public class AgentUtil {
// 检查IP4信息
if (gateway.equals("N/A")) continue;
// 添加临时路由
// 添加临时路由 过滤没有ipv4的接口
boolean flag = addTempRoute(ipv4, name, gateway);
if (!flag) continue;
@@ -557,11 +557,51 @@ public class AgentUtil {
}
/**
* dmidecode -s system-serial-number命令获取SN序列号
* @return SN序列号,如果获取失败则返回空字符串
*/
public static String getDeviceSN() {
BufferedReader reader = null;
try {
// 执行dmidecode命令获取系统序列号
Process process = Runtime.getRuntime().exec("dmidecode -s system-serial-number");
// 读取命令输出
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String sn = reader.readLine();
// 等待命令执行完成
int exitCode = process.waitFor();
// 检查命令是否执行成功且输出不为空
if (exitCode == 0 && sn != null && !sn.trim().isEmpty()) {
return sn.trim();
} else {
AssertLog.error("Failed to get SN: Command exited with code " + exitCode);
return "";
}
} catch (Exception e) {
// 处理其他未知异常
AssertLog.error("Unexpected error while getting device SN: " + e.getMessage());
return "";
} finally {
// 确保关闭资源
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
AssertLog.error("Error closing reader: " + e.getMessage());
}
}
}
}
/**
* 获取 /etc/issue 文件的第二行内容
* @return 第二行字符串,如果不存在则返回 null
*/
public static String getDeviceSN() {
public static String getDeviceSNBak() {
Path issuePath = Paths.get("/etc/issue");
if (!Files.exists(issuePath)) {
System.err.println("文件不存在: /etc/issue");
@@ -1,19 +1,46 @@
package com.tongran.agent.client.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class NetworkUtil {
public static boolean addRoute(String ip, String prefix, String gateway, String dev) {
try {
ProcessBuilder pb = new ProcessBuilder("ip", "route", "add",
ip + "/" + prefix, "via", gateway, "dev", dev);
ip + "/" + prefix, "via", gateway, "dev", dev);
pb.redirectErrorStream(true);
Process p = pb.start();
return p.waitFor() == 0;
String output = readProcessOutput(p);
int exitCode = p.waitFor();
if (exitCode == 0) {
return true;
} else {
// 检查是否是路由已存在的错误
if (output != null && (output.contains("File exists") || output.contains("RTNETLINK answers: File exists"))) {
AssertLog.info("Route already exists");
return true;
}
AssertLog.error("Failed to add route: " + output);
return false;
}
} catch (Exception e) {
e.printStackTrace();
AssertLog.error("Exception while adding route: " + e.getMessage());
return false;
}
}
private static String readProcessOutput(Process p) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
return reader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
return "Error reading output: " + e.getMessage();
}
}
public static boolean deleteRoute(String ip, String prefix, String gateway, String dev) {
try {
ProcessBuilder pb = new ProcessBuilder("ip", "route", "del",