#!/bin/bash

# 定义 ens3 和 ens9 的配置
ens3_config="
TYPE=Ethernet
BOOTPROTO=none
PEERDNS=yes
PEERROUTES=yes
NAME=ens3
DEVICE=ens3
ONBOOT=yes
IPADDR=172.25.250.1
PREFIX=24
"

ens9_config="
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
NAME=ens9
DEVICE=ens9
ONBOOT=yes
IPADDR=192.168.100.1
PREFIX=24
GATEWAY=192.168.100.254
DNS1=192.168.100.254
"

configure_network_interface() {
    local interface_name="$1"
    local config="$2"
    local config_path="/etc/sysconfig/network-scripts/ifcfg-${interface_name}"

    # 检查并更新配置文件
    if [ ! -f "$config_path" ] || ! echo "$config" | diff - "$config_path" >/dev/null; then
        echo "Updating configuration for ${interface_name}..."
        echo "$config" > "$config_path"
    else
        echo "Configuration for ${interface_name} is already up to date."
    fi
}

# 配置 ens3 和 ens9
configure_network_interface "ens3" "$ens3_config"
configure_network_interface "ens9" "$ens9_config"

# 重新启动网络服务以应用更改
# 这里假设是使用 NetworkManager，如果不是请根据实际情况调整
if ! nmcli con reload; then
    echo "Failed to reload NetworkManager connections. Please restart network services manually."
fi
