feat: 在输入lan口ip时, 增加校验

This commit is contained in:
Olia Lisa
2026-07-22 20:04:25 +08:00
parent bc0c518947
commit aedf70f75b
4 changed files with 123 additions and 4 deletions
+31 -1
View File
@@ -222,6 +222,36 @@ ask_input() {
fi
}
# 获取lan口ip
input_lan_ip() {
local default_ip="$1"
while true; do
local ip
ip=$(ask_input "请输入lan口ip" "$default_ip")
if valid_ipv4 "$ip"; then
echo "$ip"
return 0
else
echo "[错误] IP地址格式不正确,请重新输入" >&2
fi
done
}
valid_ipv4() {
local ip="$1"
echo "$ip" | awk -F. '
NF==4 &&
$1>=0 && $1<=255 &&
$2>=0 && $2<=255 &&
$3>=0 && $3<=255 &&
$4>=0 && $4<=255
'
}
# 项目根目录,保证从任意位置执行 r2s.sh 都能找到其他资源
project_dir="$(cd "$(dirname "$0")" && pwd)"
cd "$project_dir"
@@ -236,7 +266,7 @@ rootfs_partsize=$(ask_input "请输入根分区大小(MB)" "$default_rootfs_part
# lan口ip
default_lan_ip="192.168.1.1"
lan_ip=$(ask_input "请输入lanip" $default_lan_ip)
lan_ip=$(input_lan_ip "$default_lan_ip")
install_dependencies