fix:网络配置
This commit is contained in:
parent
427d4c5027
commit
392428d2f0
@ -14,6 +14,8 @@ import jakarta.validation.Valid;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "网络配置管理", description = "提供设备网络信息查询、IPv4/IPv6配置、Bond配置及路由管理等接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/device/network")
|
||||
@ -25,6 +27,12 @@ public class NetworkConfigController {
|
||||
this.networkConfigService = networkConfigService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有网卡名", description = "获取系统中所有网卡名称")
|
||||
@GetMapping("/card/eth-names")
|
||||
public ApiResponse<List<String>> getEthDeviceNames() {
|
||||
return ApiResponse.success(networkConfigService.getEthDeviceNames());
|
||||
}
|
||||
|
||||
@Operation(summary = "分页查询网卡配置", description = "分页查询设备网卡配置列表,支持按网卡名称、状态等条件筛选")
|
||||
@PostMapping("/card/Page")
|
||||
public ApiResponse<PageResult<NetworkTableResponse>> getNetworkTablePage(@RequestBody NetworkTableQueryRequest request) {
|
||||
|
||||
@ -108,6 +108,58 @@ public class NetworkConfigService {
|
||||
}
|
||||
|
||||
|
||||
public List<String> getEthDeviceNames() {
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
List<String> lines = executeCommand(
|
||||
"nmcli",
|
||||
"-t",
|
||||
"-f",
|
||||
"DEVICE,TYPE",
|
||||
"device",
|
||||
"status"
|
||||
);
|
||||
|
||||
for (String line : lines) {
|
||||
if (line == null || line.trim().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String[] parts = line.split("(?<!\\\\):", -1);
|
||||
if (parts.length < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String device = parts[0].replace("\\:", ":");
|
||||
String type = parts[1];
|
||||
|
||||
if (!"ethernet".equals(type)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!device.matches("^eth\\d+$")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(device);
|
||||
}
|
||||
|
||||
result.sort(Comparator.comparingInt(this::getEthIndex));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private int getEthIndex(String deviceName) {
|
||||
if (deviceName == null) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
String name = deviceName.trim();
|
||||
|
||||
return Integer.parseInt(name.substring(3));
|
||||
}
|
||||
|
||||
|
||||
public PageResult<NetworkTableResponse> getNetworkTablePage(NetworkTableQueryRequest req) {
|
||||
List<NetworkTableResponse> resultList = new ArrayList<>();
|
||||
String targetDeviceName = trim(req.getDeviceName());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user