#!/bin/bash
# CentOS7 Interactive Static Network Config Script
# Fix subshell array lost bug | Accurate UP detect | No empty interface
# Pure ip addr | Number select | Fixed DNS:223.6.6.6 / 223.5.5.5
set +e

# Color define
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

# Check root permission
if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED}ERROR: This script must be executed with root privileges!${NC}"
    exit 1
fi

echo -e "${GREEN}================ CentOS7 Network Auto Config Tool ================${NC}"
echo ""

# 初始化网卡数组
IF_LIST=()
INDEX=1
echo -e "${BLUE}[All Network Interfaces, UP interfaces highlighted in green]${NC}"

# 进程替换避免子Shell数组丢失
while read NIC_NAME; do
    # 判断网卡状态
    if ip addr show "$NIC_NAME" | grep -q 'state UP'; then
        echo -e "[${INDEX}] ${GREEN}${NIC_NAME}    Status: UP${NC}"
    else
        echo -e "[${INDEX}] ${NIC_NAME}    Status: DOWN"
    fi
    IF_LIST+=("${NIC_NAME}")
    ((INDEX++))
done < <(ip addr | grep -E '^[0-9]+:' | awk '{print $2}' | sed 's/://' | grep -v '^lo$')

echo ""

# 序号选择网卡
read -p "Please input the serial number of the interface you want to configure: " SEL_NUM
# 校验是否为数字
if ! [[ "${SEL_NUM}" =~ ^[0-9]+$ ]]; then
    echo -e "${RED}ERROR: Please enter a valid number!${NC}"
    exit 1
fi
ARR_INDEX=$((SEL_NUM - 1))
# 校验序号范围
if [[ ${ARR_INDEX} -lt 0 || ${ARR_INDEX} -ge ${#IF_LIST[@]} ]]; then
    echo -e "${RED}ERROR: Serial number out of range!${NC}"
    exit 1
fi

NIC="${IF_LIST[${ARR_INDEX}]}"
CFG_FILE="/etc/sysconfig/network-scripts/ifcfg-${NIC}"

if [[ ! -f "${CFG_FILE}" ]]; then
    echo -e "${RED}ERROR: Config file ${CFG_FILE} for ${NIC} does NOT exist!${NC}"
    exit 1
fi

# 备份原有配置
BAK_FILE="${CFG_FILE}.bak.$(date +%Y%m%d_%H%M%S)"
cp "${CFG_FILE}" "${BAK_FILE}"
echo -e "${CYAN}Selected Interface: ${GREEN}${NIC}${NC}"
echo -e "${CYAN}Original config backed up to: ${BAK_FILE}${NC}"
echo ""

# 录入IP、掩码、网关
read -p "Enter static IPv4 address (e.g. 192.168.1.100): " IPADDR
read -p "Enter subnet mask (e.g. 255.255.255.0): " NETMASK
read -p "Enter gateway IP (e.g. 192.168.1.1): " GATEWAY

# 固定DNS，无需输入
DNS1="223.6.6.6"
DNS2="223.5.5.5"
echo -e "${CYAN}Using fixed DNS: ${DNS1}, ${DNS2}${NC}"
echo ""

# 写入网卡配置
cat > "${CFG_FILE}" <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=${NIC}
DEVICE=${NIC}
ONBOOT=yes
IPADDR=${IPADDR}
NETMASK=${NETMASK}
GATEWAY=${GATEWAY}
DNS1=${DNS1}
DNS2=${DNS2}
EOF

echo -e "${GREEN}Successfully wrote config file: ${CFG_FILE}${NC}"
cat "${CFG_FILE}"
echo ""

# 重启网络服务
echo -e "${BLUE}Restarting network service now...${NC}"
systemctl restart network
RESTART_RET=$?
if [[ ${RESTART_RET} -ne 0 ]]; then
    echo -e "${RED}WARNING: Network service restart reported error!${NC}"
fi
echo -e "${GREEN}Network service restart process finished${NC}"
echo ""

# 连通性测试
echo -e "${BLUE}Running network connectivity test: ping qq.com (4 packets)${NC}"
ping -c 4 qq.com
PING_RET=$?

if [[ ${PING_RET} -eq 0 ]]; then
    echo -e "${GREEN}================ Network Config SUCCESS, Internet access OK ================${NC}"
else
    echo -e "${RED}================ Ping FAILED, please check IP/Gateway/Firewall/Peer Link ================${NC}"
    echo -e "${CYAN}Tip: Original config backup file: ${BAK_FILE}, you can restore it manually${NC}"
fi