From 1b3ec668030f493c35a25b1458558f1d4b7355e5 Mon Sep 17 00:00:00 2001 From: gaoyutao Date: Mon, 1 Dec 2025 11:03:56 +0800 Subject: [PATCH] =?UTF-8?q?sn=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F=E6=94=B9?= =?UTF-8?q?=E5=8F=98=EF=BC=8Cipv4=E4=B8=B4=E6=97=B6=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tongran/agent/client/utils/AgentUtil.java | 44 ++++++++++++++++++- .../agent/client/utils/NetworkUtil.java | 33 ++++++++++++-- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/tongran/agent/client/utils/AgentUtil.java b/src/main/java/com/tongran/agent/client/utils/AgentUtil.java index 06d22e1..e8e80ed 100644 --- a/src/main/java/com/tongran/agent/client/utils/AgentUtil.java +++ b/src/main/java/com/tongran/agent/client/utils/AgentUtil.java @@ -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"); diff --git a/src/main/java/com/tongran/agent/client/utils/NetworkUtil.java b/src/main/java/com/tongran/agent/client/utils/NetworkUtil.java index 0ca4130..4289e53 100644 --- a/src/main/java/com/tongran/agent/client/utils/NetworkUtil.java +++ b/src/main/java/com/tongran/agent/client/utils/NetworkUtil.java @@ -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",