diff --git a/src/main/java/com/cisd/tms/modules/device/service/NetworkConfigService.java b/src/main/java/com/cisd/tms/modules/device/service/NetworkConfigService.java index eabe726..913e76c 100644 --- a/src/main/java/com/cisd/tms/modules/device/service/NetworkConfigService.java +++ b/src/main/java/com/cisd/tms/modules/device/service/NetworkConfigService.java @@ -112,6 +112,7 @@ public class NetworkConfigService { List result = new ArrayList<>(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -164,6 +165,7 @@ public class NetworkConfigService { List result = new ArrayList<>(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -226,6 +228,7 @@ public class NetworkConfigService { int pageSize = req.getPageSize(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -286,6 +289,7 @@ public class NetworkConfigService { if ("connected".equalsIgnoreCase(state)) { List infoLines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -319,6 +323,7 @@ public class NetworkConfigService { if (connectionName != null && !connectionName.trim().isEmpty()) { try { List conLines = executeCommand( + "sudo", "nmcli", "-g", "ipv4.addresses,ipv4.gateway,ipv6.addresses,ipv6.gateway", @@ -425,6 +430,7 @@ public class NetworkConfigService { if (isNewConnection) { cmdArgs = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "add", @@ -441,6 +447,7 @@ public class NetworkConfigService { )); } else { cmdArgs = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "mod", @@ -474,14 +481,14 @@ public class NetworkConfigService { if ("Active".equalsIgnoreCase(status)) { try { - executeCommand("nmcli", "dev", "reapply", deviceName); + executeCommand("sudo", "nmcli", "dev", "reapply", deviceName); } catch (RuntimeException e) { log.warn("reapply 网卡 [{}] 失败,尝试重新激活连接 [{}]: {}", deviceName, connectionName, e.getMessage()); - executeCommand("nmcli", "con", "up", connectionName); + executeCommand("sudo", "nmcli", "con", "up", connectionName); } } else if ("Inactive".equalsIgnoreCase(status)) { try { - executeCommand("nmcli", "con", "down", connectionName); + executeCommand("sudo", "nmcli", "con", "down", connectionName); } catch (Exception e) { log.warn("设置网卡为未激活时,down网卡操作抛出异常: {}", e.getMessage()); } @@ -526,6 +533,7 @@ public class NetworkConfigService { try { List lines = executeCommand( + "sudo", "nmcli", "-g", addressesKey, @@ -574,6 +582,7 @@ public class NetworkConfigService { String newAddresses = String.join(",", remainAddressList); executeCommand( + "sudo", "nmcli", "con", "mod", @@ -630,7 +639,7 @@ public class NetworkConfigService { int pageNum = req.getPageNum(); int pageSize = req.getPageSize(); - List lines = executeCommand("nmcli", "-t", "-f", "NAME,TYPE,DEVICE", "con", "show"); + List lines = executeCommand("sudo", "nmcli", "-t", "-f", "NAME,TYPE,DEVICE", "con", "show"); for (String line : lines) { if (line == null || line.trim().isEmpty()) { @@ -665,6 +674,7 @@ public class NetworkConfigService { List slaveList = getBondSlaves(bondName); List conLines = executeCommand( + "sudo", "nmcli", "-g", "IPv4.ADDRESSES,IPv4.GATEWAY,IPv6.ADDRESSES,IPv6.GATEWAY", @@ -718,6 +728,7 @@ public class NetworkConfigService { Map result = new HashMap<>(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -765,7 +776,7 @@ public class NetworkConfigService { validateIpAndPrefix(ipAddress, gateway, prefixLength, isIpv6); - List lines = executeCommand("nmcli", "-g", "NAME", "con", "show"); + List lines = executeCommand("sudo", "nmcli", "-g", "NAME", "con", "show"); for (String line : lines) { if (bondName.equals(line.trim())) { throw new BizException( @@ -786,6 +797,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "add", @@ -800,6 +812,7 @@ public class NetworkConfigService { ); List ipCmdArgs = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "mod", @@ -838,6 +851,7 @@ public class NetworkConfigService { oldConnectionSnapshots.addAll(downOldConnectionByDevice(slaveName)); executeCommand( + "sudo", "nmcli", "con", "add", @@ -854,7 +868,7 @@ public class NetworkConfigService { createdSlaveConnectionNames.add(slaveConnectionName); } - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } catch (RuntimeException e) { rollbackCreateBond(bondName, createdSlaveConnectionNames, oldConnectionSnapshots); @@ -893,6 +907,7 @@ public class NetworkConfigService { String bondOptions = String.format("mode=%s,miimon=100", mapBondMode(mode)); executeCommand( + "sudo", "nmcli", "con", "mod", @@ -902,6 +917,7 @@ public class NetworkConfigService { ); List ipCmdArgs = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "mod", @@ -931,10 +947,10 @@ public class NetworkConfigService { syncBondSlaves(bondName, newSlaveList); if ("Active".equalsIgnoreCase(status)) { - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } else if ("Inactive".equalsIgnoreCase(status)) { try { - executeCommand("nmcli", "con", "down", bondName); + executeCommand("sudo", "nmcli", "con", "down", bondName); } catch (RuntimeException e) { log.warn("停用 Bond [{}] 失败: {}", bondName, e.getMessage()); } @@ -967,6 +983,7 @@ public class NetworkConfigService { try { List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -997,6 +1014,7 @@ public class NetworkConfigService { if ("ethernet".equals(type) || "802-3-ethernet".equals(type)) { try { List masterLines = executeCommand( + "sudo", "nmcli", "-g", "connection.master", @@ -1026,13 +1044,13 @@ public class NetworkConfigService { for (String uuid : uuidToDeleteList) { try { - executeCommand("nmcli", "con", "down", "uuid", uuid); + executeCommand("sudo", "nmcli", "con", "down", "uuid", uuid); } catch (RuntimeException e) { log.warn("停用 Bond 从属连接 [{}] 失败,继续删除: {}", uuid, e.getMessage()); } try { - executeCommand("nmcli", "con", "delete", "uuid", uuid); + executeCommand("sudo", "nmcli", "con", "delete", "uuid", uuid); } catch (RuntimeException e) { throw new BizException( ErrorCode.BIZ_ERROR.getCode(), @@ -1042,12 +1060,12 @@ public class NetworkConfigService { } try { - executeCommand("nmcli", "con", "down", "uuid", bondUuid); + executeCommand("sudo", "nmcli", "con", "down", "uuid", bondUuid); } catch (RuntimeException e) { log.warn("停用 Bond 主连接 [{}] 失败,继续删除: {}", bondName, e.getMessage()); } - executeCommand("nmcli", "con", "delete", "uuid", bondUuid); + executeCommand("sudo", "nmcli", "con", "delete", "uuid", bondUuid); } catch (BizException e) { throw e; @@ -1118,6 +1136,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1224,6 +1243,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1233,6 +1253,7 @@ public class NetworkConfigService { ); executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1289,6 +1310,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1314,6 +1336,7 @@ public class NetworkConfigService { Set result = new HashSet<>(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -1352,6 +1375,7 @@ public class NetworkConfigService { private void clearProtocolConfig(String connectionName, String protocol) { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1439,6 +1463,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1500,6 +1525,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1525,6 +1551,7 @@ public class NetworkConfigService { try { List lines = executeCommand( + "sudo", "nmcli", "-g", routeKey, @@ -1553,6 +1580,7 @@ public class NetworkConfigService { try { if (snapshot.bondOptions != null && !snapshot.bondOptions.trim().isEmpty()) { executeCommand( + "sudo", "nmcli", "con", "mod", @@ -1567,6 +1595,7 @@ public class NetworkConfigService { try { List ipv4Args = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "mod", @@ -1588,6 +1617,7 @@ public class NetworkConfigService { try { List ipv6Args = new ArrayList<>(Arrays.asList( + "sudo", "nmcli", "con", "mod", @@ -1615,10 +1645,10 @@ public class NetworkConfigService { try { if (snapshot.active) { - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } else { try { - executeCommand("nmcli", "con", "down", bondName); + executeCommand("sudo", "nmcli", "con", "down", bondName); } catch (Exception e) { log.warn("回滚停用 Bond [{}] 失败: {}", bondName, e.getMessage()); } @@ -1631,6 +1661,7 @@ public class NetworkConfigService { private BondConfigSnapshot snapshotBondConfig(String bondName) { List lines = executeCommand( + "sudo", "nmcli", "-g", "bond.options,IPv4.METHOD,IPv4.ADDRESSES,IPv4.GATEWAY,IPv6.METHOD,IPv6.ADDRESSES,IPv6.GATEWAY", @@ -1685,6 +1716,7 @@ public class NetworkConfigService { } List activeUuids = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -1695,6 +1727,7 @@ public class NetworkConfigService { ); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -1726,6 +1759,7 @@ public class NetworkConfigService { String interfaceName = ""; try { List ifLines = executeCommand( + "sudo", "nmcli", "-g", "connection.interface-name", @@ -1750,6 +1784,7 @@ public class NetworkConfigService { String autoconnect = "yes"; try { List autoLines = executeCommand( + "sudo", "nmcli", "-g", "connection.autoconnect", @@ -1773,6 +1808,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "modify", @@ -1788,7 +1824,7 @@ public class NetworkConfigService { if (active) { try { - executeCommand("nmcli", "con", "down", "uuid", uuid); + executeCommand("sudo", "nmcli", "con", "down", "uuid", uuid); } catch (RuntimeException e) { log.warn("停用旧连接 [{}] 失败,uuid: {}, 原因: {}", name, uuid, e.getMessage()); } @@ -1886,6 +1922,7 @@ public class NetworkConfigService { try { List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -1916,6 +1953,7 @@ public class NetworkConfigService { String interfaceName = ""; try { List interfaceLines = executeCommand( + "sudo", "nmcli", "-g", "connection.interface-name", @@ -1971,13 +2009,13 @@ public class NetworkConfigService { } try { - executeCommand("nmcli", "con", "down", slaveConnectionName); + executeCommand("sudo", "nmcli", "con", "down", slaveConnectionName); } catch (Exception e) { log.warn("回滚停用 Bond 从属连接失败: {}, 原因: {}", slaveConnectionName, e.getMessage()); } try { - executeCommand("nmcli", "con", "delete", slaveConnectionName); + executeCommand("sudo", "nmcli", "con", "delete", slaveConnectionName); } catch (Exception e) { log.warn("回滚删除 Bond 从属连接失败: {}, 原因: {}", slaveConnectionName, e.getMessage()); } @@ -1987,13 +2025,13 @@ public class NetworkConfigService { if (bondName != null && !bondName.trim().isEmpty()) { try { - executeCommand("nmcli", "con", "down", bondName); + executeCommand("sudo", "nmcli", "con", "down", bondName); } catch (Exception e) { log.warn("回滚停用 Bond 主连接失败: {}, 原因: {}", bondName, e.getMessage()); } try { - executeCommand("nmcli", "con", "delete", bondName); + executeCommand("sudo", "nmcli", "con", "delete", bondName); } catch (Exception e) { log.warn("回滚删除 Bond 主连接失败: {}, 原因: {}", bondName, e.getMessage()); } @@ -2013,6 +2051,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "modify", @@ -2027,7 +2066,7 @@ public class NetworkConfigService { if (snapshot.active) { try { - executeCommand("nmcli", "con", "up", "uuid", uuid); + executeCommand("sudo", "nmcli", "con", "up", "uuid", uuid); } catch (Exception e) { log.warn("回滚恢复旧连接激活失败,uuid: {}, 原因: {}", uuid, e.getMessage()); } @@ -2046,6 +2085,7 @@ public class NetworkConfigService { try { List lines = executeCommand( + "sudo", "nmcli", "-g", "connection.type", @@ -2108,7 +2148,7 @@ public class NetworkConfigService { private void checkSlaveDevicesExist(List slaveList) { - List deviceLines = executeCommand("nmcli", "-t", "-f", "DEVICE,TYPE", "dev"); + List deviceLines = executeCommand("sudo", "nmcli", "-t", "-f", "DEVICE,TYPE", "dev"); for (String slave : slaveList) { String slaveName = slave.trim(); @@ -2153,6 +2193,7 @@ public class NetworkConfigService { private boolean isConnectionActive(String connectionName) { try { List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -2274,6 +2315,7 @@ public class NetworkConfigService { Map statusMap = new HashMap<>(); List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -2361,10 +2403,10 @@ public class NetworkConfigService { private void applyConnection(String connectionName, String interfaceName) { try { - executeCommand("nmcli", "dev", "reapply", interfaceName); + executeCommand("sudo", "nmcli", "dev", "reapply", interfaceName); } catch (RuntimeException e) { log.warn("reapply 网口 [{}] 失败,尝试重新激活连接 [{}]: {}", interfaceName, connectionName, e.getMessage()); - executeCommand("nmcli", "con", "up", connectionName); + executeCommand("sudo", "nmcli", "con", "up", connectionName); } } @@ -2401,6 +2443,7 @@ public class NetworkConfigService { oldConnectionSnapshots.addAll(downOldConnectionByDevice(slaveName)); executeCommand( + "sudo", "nmcli", "con", "add", @@ -2417,7 +2460,7 @@ public class NetworkConfigService { createdSlaveConnectionNames.add(slaveConnectionName); } - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } catch (RuntimeException e) { rollbackAddSlavesToBond(createdSlaveConnectionNames, oldConnectionSnapshots); @@ -2464,18 +2507,18 @@ public class NetworkConfigService { try { for (BondSlaveSnapshot snapshot : targetSlaveSnapshots) { try { - executeCommand("nmcli", "con", "down", "uuid", snapshot.uuid); + executeCommand("sudo", "nmcli", "con", "down", "uuid", snapshot.uuid); } catch (Exception e) { log.warn("停用从属网卡连接 [{}] 失败,忽略并继续删除: {}", snapshot.name, e.getMessage()); } - executeCommand("nmcli", "con", "delete", "uuid", snapshot.uuid); + executeCommand("sudo", "nmcli", "con", "delete", "uuid", snapshot.uuid); deletedSlaveSnapshots.add(snapshot); } if (!deletedSlaveSnapshots.isEmpty()) { try { - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } catch (RuntimeException e) { rollbackDeletedBondSlaves(bondName, deletedSlaveSnapshots); throw new BizException( @@ -2508,6 +2551,7 @@ public class NetworkConfigService { } List lines = executeCommand( + "sudo", "nmcli", "-t", "-f", @@ -2562,6 +2606,7 @@ public class NetworkConfigService { try { List detailLines = executeCommand( + "sudo", "nmcli", "-g", "connection.master,connection.interface-name", @@ -2627,6 +2672,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "add", @@ -2651,7 +2697,7 @@ public class NetworkConfigService { } try { - executeCommand("nmcli", "con", "up", bondName); + executeCommand("sudo", "nmcli", "con", "up", bondName); } catch (Exception e) { log.warn("恢复 Bond [{}] 激活失败: {}", bondName, e.getMessage()); } @@ -2668,13 +2714,13 @@ public class NetworkConfigService { } try { - executeCommand("nmcli", "con", "down", slaveConnectionName); + executeCommand("sudo", "nmcli", "con", "down", slaveConnectionName); } catch (Exception e) { log.warn("回滚停用 Bond 从属连接 [{}] 失败: {}", slaveConnectionName, e.getMessage()); } try { - executeCommand("nmcli", "con", "delete", slaveConnectionName); + executeCommand("sudo", "nmcli", "con", "delete", slaveConnectionName); } catch (Exception e) { log.warn("回滚删除 Bond 从属连接 [{}] 失败: {}", slaveConnectionName, e.getMessage()); } @@ -2686,7 +2732,7 @@ public class NetworkConfigService { private void checkConnectionNameNotExists(String connectionName) { try { - executeCommand("nmcli", "con", "show", connectionName); + executeCommand("sudo", "nmcli", "con", "show", connectionName); throw new BizException( ErrorCode.BIZ_ERROR.getCode(), @@ -2717,6 +2763,7 @@ public class NetworkConfigService { try { executeCommand( + "sudo", "nmcli", "con", "modify", @@ -2731,7 +2778,7 @@ public class NetworkConfigService { if (snapshot.active) { try { - executeCommand("nmcli", "con", "up", "uuid", uuid); + executeCommand("sudo", "nmcli", "con", "up", "uuid", uuid); } catch (Exception e) { log.warn("恢复旧连接激活失败,uuid: {}, 原因: {}", uuid, e.getMessage()); } @@ -2781,7 +2828,7 @@ public class NetworkConfigService { List slaveList = new ArrayList<>(); List lines; try { - lines = executeCommand("nmcli", "-t", "-f", "UUID,DEVICE,NAME,TYPE", "con", "show"); + lines = executeCommand("sudo", "nmcli", "-t", "-f", "UUID,DEVICE,NAME,TYPE", "con", "show"); } catch (RuntimeException e) { throw new BizException(ErrorCode.BIZ_ERROR.getCode(), "查询 Bond 从属网卡列表失败"); } @@ -2805,7 +2852,7 @@ public class NetworkConfigService { } if (type != null && type.contains("ethernet")) { - List masterOutputs = executeCommand("nmcli", "-g", "connection.master", "con", "show", uuid); + List masterOutputs = executeCommand("sudo", "nmcli", "-g", "connection.master", "con", "show", uuid); String master = masterOutputs.isEmpty() ? "" : masterOutputs.get(0).trim(); if (bondName.equals(master)) { @@ -2837,7 +2884,7 @@ public class NetworkConfigService { List lines; try { - lines = executeCommand("nmcli", "-g", "bond.options", "con", "show", bondName); + lines = executeCommand("sudo", "nmcli", "-g", "bond.options", "con", "show", bondName); } catch (RuntimeException e) { throw new BizException(ErrorCode.BIZ_ERROR.getCode(), "读取 Bond 配置信息失败,请检查名称 [" + bondName + "] 是否存在"); } @@ -2874,7 +2921,7 @@ public class NetworkConfigService { private String getConnectionNameByDeviceName(String deviceName) { if (deviceName == null || deviceName.trim().isEmpty()) return null; try { - List lines = executeCommand("nmcli", "-t", "-f", "DEVICE,CONNECTION", "dev", "status"); + List lines = executeCommand("sudo", "nmcli", "-t", "-f", "DEVICE,CONNECTION", "dev", "status"); for (String line : lines) { String[] parts = line.split("(?