#!/bin/bash

# 检查脚本是否以 root 权限运行
if [ "$EUID" -ne 0 ]; then
    echo "请以 root 权限运行此脚本。"
    exit 1
fi

# 检查系统发行版类型
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO=$ID
else
    echo "无法检测系统发行版。"
    exit 1
fi

# 检查是否安装了 NetworkManager 或 ip 命令
if ! command -v nmcli &>/dev/null && ! command -v ip &>/dev/null; then
    echo "需要安装 NetworkManager 或 iproute2 工具。"
    exit 1
fi

# 定义函数：读取当前网络配置
get_network_config() {
    echo "========================="
    echo "  当前网络配置"
    echo "========================="

    # 初始化变量
    INTERFACE=""
    CONNECTION_NAME=""
    IP_ADDRESS=""
    GATEWAY=""
    DNS=""

    # 获取网络接口名称
    if command -v nmcli &>/dev/null; then
        INTERFACE=$(nmcli -t -f DEVICE,STATE device status | grep -w "connected" | cut -d':' -f1 | head -n 1)
        CONNECTION_NAME=$(nmcli -t -f NAME,DEVICE connection show --active | grep "$INTERFACE" | cut -d':' -f1)

        if [ -z "$CONNECTION_NAME" ]; then
            CONNECTION_NAME="$INTERFACE"
            nmcli connection add type ethernet ifname "$INTERFACE" con-name "$CONNECTION_NAME" autoconnect yes
            echo "创建了新的连接配置: $CONNECTION_NAME"
        fi

        IP_ADDRESS=$(nmcli -g IP4.ADDRESS dev show "$INTERFACE" | awk -F'/' '{print $1}')
        GATEWAY=$(nmcli -g IP4.GATEWAY dev show "$INTERFACE")
        DNS=$(nmcli -g IP4.DNS dev show "$INTERFACE")
    fi

    # 如果 nmcli 获取失败，则使用 ip 命令
    if [ -z "$INTERFACE" ] && command -v ip &>/dev/null; then
        INTERFACE=$(ip -o -4 route show to default | awk '{print $5}')
        IP_ADDRESS=$(ip -4 addr show "$INTERFACE" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
        GATEWAY=$(ip route | grep default | awk '{print $3}')
        DNS=$(grep "nameserver" /etc/resolv.conf | head -n 1 | awk '{print $2}')
    fi

    if [ -z "$INTERFACE" ]; then
        echo "未能检测到活动的网络接口。"
        exit 1
    fi

    echo "网络接口: $INTERFACE"
    echo "IP 地址: ${IP_ADDRESS:-未设置}"
    echo "网关: ${GATEWAY:-未设置}"
    echo "DNS: ${DNS:-未设置}"
}

# 定义函数：显示菜单
show_menu() {
    echo "========================="
    echo "  网络配置修改菜单"
    echo "========================="
    echo "1. 修改 IP 地址"
    echo "2. 修改 网关"
    echo "3. 退出"
    echo "========================="
}

# 修改 IP 地址
modify_ip() {
    read -p "请输入新的 IP 地址 (例如 192.168.1.100): " NEW_IP
    read -p "请输入子网掩码前缀 (例如 24): " SUBNET_PREFIX
    if command -v nmcli &>/dev/null; then
        nmcli con mod "$CONNECTION_NAME" ipv4.addresses "$NEW_IP/$SUBNET_PREFIX"
        nmcli con mod "$CONNECTION_NAME" ipv4.method manual
        nmcli con up "$CONNECTION_NAME"
    elif command -v ip &>/dev/null; then
        ip addr add "$NEW_IP/$SUBNET_PREFIX" dev "$INTERFACE"
    else
        echo "系统不支持网络管理命令，请安装 NetworkManager 或 iproute2 工具。"
    fi
    echo "IP 地址已修改为 $NEW_IP"
}

# 修改网关
modify_gateway() {
    read -p "请输入新的网关 (例如 192.168.1.1): " NEW_GATEWAY
    if command -v nmcli &>/dev/null; then
        nmcli con mod "$CONNECTION_NAME" ipv4.gateway "$NEW_GATEWAY"
        nmcli con up "$CONNECTION_NAME"
    elif command -v ip &>/dev/null; then
        ip route replace default via "$NEW_GATEWAY" dev "$INTERFACE"
    else
        echo "系统不支持网络管理命令，请安装 NetworkManager 或 iproute2 工具。"
    fi
    echo "网关已修改为 $NEW_GATEWAY"
}

# 主程序
get_network_config

while true; do
    show_menu
    read -p "请选择一个选项 (1-3): " OPTION
    case $OPTION in
        1)
            modify_ip
            ;;
        2)
            modify_gateway
            ;;
        3)
            echo "退出程序"
            exit 0
            ;;
        *)
            echo "无效选项，请重新选择。"
            ;;
    esac
    # 显示更新后的网络配置
    get_network_config
done
