v1.1初始化
This commit is contained in:
@@ -16,6 +16,7 @@ import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AgentUtil {
|
||||
|
||||
@@ -552,4 +553,31 @@ public class AgentUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getNetworkParam(String filePath, String key){
|
||||
Properties props = new Properties();
|
||||
String result = "";
|
||||
try (InputStream input = Files.newInputStream(Paths.get(filePath))) {
|
||||
// 加载配置文件
|
||||
props.load(input);
|
||||
result = props.getProperty(key, "");
|
||||
System.out.println("配置文件加载成功");
|
||||
} catch (IOException e) {
|
||||
System.err.println("无法加载配置文件,使用默认值");
|
||||
} catch (NumberFormatException e) {
|
||||
System.err.println("配置文件格式错误: " + e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件,跳过空行,将所有非空行拼接成一个字符串
|
||||
* @param filePath 文件路径
|
||||
* @return 拼接后的字符串(空行已去除)
|
||||
*/
|
||||
public static String readNonEmptyLinesAsString(String filePath) throws Exception {
|
||||
return Files.lines(Paths.get(filePath))
|
||||
.filter(line -> !line.trim().isEmpty()) // 去除 null 和 空/空白行
|
||||
.collect(Collectors.joining("\n")); // 用换行符连接
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.tongran.agent.client.utils;
|
||||
|
||||
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);
|
||||
pb.redirectErrorStream(true);
|
||||
Process p = pb.start();
|
||||
return p.waitFor() == 0;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean deleteRoute(String ip, String prefix, String gateway, String dev) {
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder("ip", "route", "del",
|
||||
ip + "/" + prefix, "via", gateway, "dev", dev);
|
||||
return pb.start().waitFor() == 0;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user