sn获取方式改变,ipv4临时路由添加异常处理完善
This commit is contained in:
@@ -298,7 +298,7 @@ public class AgentUtil {
|
|||||||
// 检查IP4信息
|
// 检查IP4信息
|
||||||
if (gateway.equals("N/A")) continue;
|
if (gateway.equals("N/A")) continue;
|
||||||
|
|
||||||
// 添加临时路由
|
// 添加临时路由 过滤没有ipv4的接口
|
||||||
boolean flag = addTempRoute(ipv4, name, gateway);
|
boolean flag = addTempRoute(ipv4, name, gateway);
|
||||||
if (!flag) continue;
|
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 文件的第二行内容
|
* 获取 /etc/issue 文件的第二行内容
|
||||||
* @return 第二行字符串,如果不存在则返回 null
|
* @return 第二行字符串,如果不存在则返回 null
|
||||||
*/
|
*/
|
||||||
public static String getDeviceSN() {
|
public static String getDeviceSNBak() {
|
||||||
Path issuePath = Paths.get("/etc/issue");
|
Path issuePath = Paths.get("/etc/issue");
|
||||||
if (!Files.exists(issuePath)) {
|
if (!Files.exists(issuePath)) {
|
||||||
System.err.println("文件不存在: /etc/issue");
|
System.err.println("文件不存在: /etc/issue");
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package com.tongran.agent.client.utils;
|
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 class NetworkUtil {
|
||||||
public static boolean addRoute(String ip, String prefix, String gateway, String dev) {
|
public static boolean addRoute(String ip, String prefix, String gateway, String dev) {
|
||||||
try {
|
try {
|
||||||
@@ -7,11 +12,33 @@ public class NetworkUtil {
|
|||||||
ip + "/" + prefix, "via", gateway, "dev", dev);
|
ip + "/" + prefix, "via", gateway, "dev", dev);
|
||||||
pb.redirectErrorStream(true);
|
pb.redirectErrorStream(true);
|
||||||
Process p = pb.start();
|
Process p = pb.start();
|
||||||
return p.waitFor() == 0;
|
|
||||||
} catch (Exception e) {
|
String output = readProcessOutput(p);
|
||||||
e.printStackTrace();
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
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) {
|
public static boolean deleteRoute(String ip, String prefix, String gateway, String dev) {
|
||||||
|
|||||||
Reference in New Issue
Block a user