From ca0de3ed66a498528e4b042e9c26b54ed131f59d Mon Sep 17 00:00:00 2001 From: Olia Lisa Date: Mon, 13 Jul 2026 15:31:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9ip=E8=84=9A=E6=9C=AC,?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E5=AF=B925.12=E4=BB=A5=E4=B8=8B=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E6=94=AF=E6=8C=81,=20=E6=AF=94=E5=A6=8224.10?= =?UTF-8?q?=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- files/etc/uci-defaults/98-network-setting | 46 ++++++++++++++++++----- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/files/etc/uci-defaults/98-network-setting b/files/etc/uci-defaults/98-network-setting index ad354f4..306ce49 100755 --- a/files/etc/uci-defaults/98-network-setting +++ b/files/etc/uci-defaults/98-network-setting @@ -2,6 +2,28 @@ # 仅在首次启动时执行(/etc/uci-defaults机制) +# 获取当前 OpenWrt/ImmortalWrt 的主版本号 +get_openwrt_version() { + local version="" + if [ -f /etc/openwrt_release ]; then + # 提取形如 25.12 或 23.05 的前两位数字版本 + version=$(grep "DISTRIB_RELEASE" /etc/openwrt_release | cut -d"'" -f2 | cut -d'.' -f1,2) + fi + echo "$version" +} + +# 比较版本号是否大于或等于 25.12 +# 返回 0 表示 >= 25.12,返回 1 表示较旧版本 +is_version_25_12_or_newer() { + local current_ver=$(get_openwrt_version) + + # 如果没获取到版本号,保守起见当做旧版本处理 + [ -z "$current_ver" ] && return 1 + + # 使用 awk 进行浮点数版本比较 + awk -v cur="$current_ver" 'BEGIN { if (cur >= 25.12) exit 0; else exit 1 }' +} + # 修改 LAN 口 IP 地址 update_lan_ip(){ local NEW_LAN_IP="$1" @@ -12,26 +34,32 @@ update_lan_ip(){ # 检查 lan 接口是否存在 if uci get network.lan >/dev/null 2>&1; then - # 修改 LAN IP - uci set network.lan.ipaddr="${NEW_LAN_IP}/24" + # 设置为静态 IP 协议 + uci set network.lan.proto='static' + + # 根据版本号执行不同的设置逻辑 + if is_version_25_12_or_newer; then + # 25.12+ 版本:合并为 CIDR 格式,并清除可能残留的旧 netmask + uci set network.lan.ipaddr="${NEW_LAN_IP}/24" + uci del network.lan.netmask >/dev/null 2>&1 + else + # 25.12 以下旧版本:拆分为 ipaddr 和 netmask + uci set network.lan.ipaddr="${NEW_LAN_IP}" + uci set network.lan.netmask="255.255.255.0" + fi # 保存配置 uci commit network - fi - fi - # 重启网络服务 if [ -x /etc/init.d/network ]; then /etc/init.d/network restart fi } - -# 启用 LAN IP 修改 +# 启用 LAN IP 修改(根据具体需求解除注释) # update_lan_ip "192.168.50.100" - -exit 0 +exit 0 \ No newline at end of file