feat: 添加切换为 DHCP 自动获取 IP 功能

- 在主菜单新增选项3:切换为 DHCP 自动获取 IP
- 支持 NetworkManager、netplan、传统 interfaces 三种网络管理方式
- 自动备份原有网络配置
- 切换前显示警告提示

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-01 12:51:54 +08:00
parent 0c00dcac58
commit 794f848900

View File

@@ -447,6 +447,169 @@ EOF
success "传统网络配置完成"
}
# 配置 DHCP 自动获取 IP
configure_dhcp() {
echo ""
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo -e "${YELLOW} 切换为 DHCP 自动获取 IP ${NC}"
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo ""
local current_ip=$(get_ip)
local current_interface=$(get_interface)
echo -e "${BLUE}当前网络配置:${NC}"
echo " 接口名称: $current_interface"
echo " 当前 IP: $current_ip"
echo ""
echo -e "${YELLOW}警告: 切换为 DHCP 后IP 地址可能会改变!${NC}"
echo " 请确保你能通过其他方式(如显示器/串口)访问设备"
echo ""
read -p "确定要切<E8A681><E58887>为 DHCP 吗? [y/N]: " confirm < /dev/tty
confirm=${confirm:-N}
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "已取消"
return 0
fi
# 检查是否使用 NetworkManager
if systemctl is-active --quiet NetworkManager 2>/dev/null; then
configure_dhcp_networkmanager "$current_interface"
# 检查是否使用 netplan (Ubuntu 18.04+)
elif [ -d /etc/netplan ]; then
configure_dhcp_netplan "$current_interface"
# 使用传统的 /etc/network/interfaces
else
configure_dhcp_interfaces "$current_interface"
fi
show_dhcp_result
}
# 使用 NetworkManager 配置 DHCP
configure_dhcp_networkmanager() {
local iface=$1
info "检测到 NetworkManager使用 nmcli 配置 DHCP..."
# 获取连接名称
local conn_name=$(nmcli -t -f NAME,DEVICE con show --active | grep "$iface" | cut -d':' -f1)
if [ -z "$conn_name" ]; then
conn_name="$iface"
fi
# 配置 DHCP
nmcli con mod "$conn_name" ipv4.method auto
nmcli con mod "$conn_name" ipv4.addresses ""
nmcli con mod "$conn_name" ipv4.gateway ""
nmcli con mod "$conn_name" ipv4.dns ""
# 重启连接
nmcli con down "$conn_name" 2>/dev/null || true
nmcli con up "$conn_name"
success "NetworkManager DHCP 配置完成"
}
# 使用 netplan 配置 DHCP
configure_dhcp_netplan() {
local iface=$1
info "检测到 netplan创建 DHCP 配置..."
# 备份现有配置
for f in /etc/netplan/*.yaml; do
if [ -f "$f" ]; then
cp "$f" "$f.bak"
info "已备份: $f -> $f.bak"
fi
done
# 创建新的 netplan DHCP 配置
cat > /etc/netplan/01-dhcp.yaml << EOF
# DHCP 配置 - 由 CUPS 一键脚本生成
network:
version: 2
renderer: networkd
ethernets:
$iface:
dhcp4: yes
EOF
# 设置正确的权限
chmod 600 /etc/netplan/01-dhcp.yaml
# 删除其他可能冲突的配置
for f in /etc/netplan/*.yaml; do
if [ "$f" != "/etc/netplan/01-dhcp.yaml" ] && [ -f "$f" ]; then
rm -f "$f"
info "移除冲突配置: $f"
fi
done
# 应用配置
info "应用 netplan 配置..."
netplan apply
success "Netplan DHCP 配置完成"
}
# 使用传统 interfaces 文件配置 DHCP
configure_dhcp_interfaces() {
local iface=$1
info "使用传统 /etc/network/interfaces 配置 DHCP..."
# 备份原配置
if [ -f /etc/network/interfaces ]; then
cp /etc/network/interfaces /etc/network/interfaces.bak
info "已备份: /etc/network/interfaces -> /etc/network/interfaces.bak"
fi
# 创建新配置
cat > /etc/network/interfaces << EOF
# DHCP 配置 - 由 CUPS 一键脚本生成
# 备份文件: /etc/network/interfaces.bak
# 本地回环接口
auto lo
iface lo inet loopback
# 主网络接口 - DHCP
auto $iface
iface $iface inet dhcp
EOF
# 重启网络服务
info "重启网络服务..."
systemctl restart networking 2>/dev/null || /etc/init.d/networking restart 2>/dev/null || true
success "传统网络配置完成"
}
# 显示 DHCP 配置结果
show_dhcp_result() {
# 等待网络重新配置
sleep 3
local new_ip=$(get_ip)
local new_interface=$(get_interface)
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ DHCP 配置完成! ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${BLUE}网络接口:${NC} $new_interface"
echo -e " ${BLUE}当前 IP:${NC} $new_ip"
echo ""
echo -e " ${YELLOW}注意:${NC} IP 地址现在由 DHCP 服务器自动分配"
echo -e " ${YELLOW} ${NC} 重启后 IP 可能会改变"
echo ""
}
# 显示静态IP配置结果
show_static_ip_result() {
local new_ip=$(get_ip)
@@ -1266,9 +1429,10 @@ show_menu() {
echo ""
echo " 1) 安装 CUPS 打印服务"
echo " 2) 卸载 CUPS 打印服务"
echo " 3) 切换为 DHCP 自动获取 IP"
echo " 0) 退出"
echo ""
read -p " 请输入选项 [1/2/0]: " choice < /dev/tty
read -p " 请输入选项 [1/2/3/0]: " choice < /dev/tty
case "$choice" in
1)
@@ -1277,6 +1441,13 @@ show_menu() {
2)
uninstall
;;
3)
check_root
configure_dhcp
echo ""
read -p "按 Enter 返回主菜单..." < /dev/tty
show_menu
;;
0)
echo "已退出"
exit 0