#!/bin/bash
# 2020-07-18-15:47:18
# ipaddr change v2
# by bubble_xu

# 脚本结束后显示感谢信息
echo -e "\033[32m-------欢迎使用咕噜云网卡修改脚本-------\033[0m"
 
echo "脚本仅支持CentOS7，其他系统不支持"
# 输出当前系统版本和内核信息
echo "当前系统版本：$(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2)"
echo "当前内核版本：$(uname -r)"

################################
NETWORK_PATH="/etc/sysconfig/network-scripts"

#if execute user root
if [ ! $UID -eq 0 ];then
	echo -e "\033[31m用户必须是root用户！\033[0m"
	exit
fi

#choose one network device
DEVICE_LIST=`ifconfig|grep flags|grep -v lo|cut -d":" -f1`
PS3=`echo -e "\033[32m请选择网卡，输入网卡前的编号（通常为第一个）:\033[0m"`
select device in $DEVICE_LIST
do
	if [ ! -z $device ];then
		echo -e "\033[33m-----您选择的网卡为： $device-------\033[0m"
		break
	fi
	echo -e "\033[31m无效的选项\033[0m"
done

# Output current network configuration
echo -e "\033[32m-------当前网卡配置信息-------\033[0m"
cat $NETWORK_PATH/ifcfg-$device

# Delete existing network configuration file
if [ -f $NETWORK_PATH/ifcfg-$device ];then
	mv $NETWORK_PATH/ifcfg-$device $NETWORK_PATH/ifcfg-${device}.bak
	echo -e "\033[33m已删除已存在的配置文件：$NETWORK_PATH/ifcfg-$device\033[0m"
fi

#create network file
function config_network(){
	mode=$1
	cat>$NETWORK_PATH/ifcfg-${device}<<-EOF
	TYPE="Ethernet"
	PROXY_METHOD="none"
	BROWSER_ONLY="no"
	BOOTPROTO="$mode"
	DEFROUTE="yes"
	IPV4_FAILURE_FATAL="no"
	IPV6INIT="yes"
	IPV6_AUTOCONF="yes"
	IPV6_DEFROUTE="yes"
	IPV6_FAILURE_FATAL="no"
	IPV6_ADDR_GEN_MODE="stable-privacy"
	NAME="$device"
	UUID="$uuid"
	DEVICE="$device"
	ONBOOT="yes"
	EOF
	if [ $mode == "static" ];then
		cat>>$NETWORK_PATH/ifcfg-${device}<<-EOF
		IPADDR=$ipaddr
		NETMASK=$netmask
		GATEWAY=$gateway
		DNS1=114.114.114.114
		DNS2=8.8.8.8
		EOF
	fi
}

#backup network device file
if [ -f $NETWORK_PATH/ifcfg-${device}.bak ];then
	uuid=`grep UUID $NETWORK_PATH/ifcfg-${device}.bak |sed 's/UUID=//g;s/"//g'`
fi


################################
NETWORK_PATH="/etc/sysconfig/network-scripts"

# 获取当前系统的IP地址、子网掩码和网关信息
CURRENT_IP=$(ip addr show dev $device | awk '/inet / {print $2}')
CURRENT_NETMASK=$(ip addr show dev $device | awk '/inet / {print $4}')
 
CURRENT_GATEWAY=$(ip route show default | awk '/default via/ {print $3}')

#choose mode,ipaddr,netmask,gateway
while true
do
echo -e "\033[32m--网卡配置已删除，操作到这里必须往下继续配置网卡信息，否则将会无法联网--\033[0m"

    read -p "请输入网络模式（请输入1表示static 固定IP，或2表示dhcp 动态IP）：" mode
    if [ $mode -eq 1 ];then
		while true
		do
		echo "当前IP地址：$CURRENT_IP"
        	read -p "请输入IP地址（请设置同网段IP）：" ipaddr
			echo $ipaddr | grep "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" >/dev/null 2>&1
			if [ $? -eq 0 ];then
				break
			fi
			echo -e "\033[31m无效的IP地址\033[0m"
		done 
		 
        read -p "请输入网络掩码（通常为255.255.255.0，参考上方掩码信息）：" netmask
        echo "当前网关：$CURRENT_GATEWAY"
        read -p "请输入网关(请配置和输出的一样)：" gateway
        config_network "static"
        break
    elif [ $mode -eq 2 ];then
        config_network "dhcp"
        break
    fi
	echo -e "\033[31m无效的模式选项\033[0m"
done 

#confirm network config info
echo -e "\033[32m-------确认网络配置信息-------\033[0m"
cat $NETWORK_PATH/ifcfg-$device
while true
do
	read -p "请确认网卡配置信息，输入1确认，输入2放弃：" option
	if [ $option -eq 2 ];then 
		if [ -f $NETWORK_PATH/ifcfg-${device}.bak ];then
			mv $NETWORK_PATH/ifcfg-${device}.bak $NETWORK_PATH/ifcfg-$device
			echo -e "\033[33m已恢复之前的配置文件：$NETWORK_PATH/ifcfg-${device}.bak\033[0m"
		fi
		echo -e "\033[33m----结束IP更改配置---\033[0m"
		exit
	elif [ $option -eq 1 ];then
		echo -e "\033[33m----启动IP更改配置---\033[0m"
		break
	fi
	echo -e "\033[31m无效的选项\033[0m"
done

#restart network
systemctl restart network
ping -c 2 baidu.com
if [ $? -eq 0 ];then
    echo -e "\033[32mIP地址更改成功！\033[0m"
    if [ -f $NETWORK_PATH/ifcfg-${device}.bak ];then
    	rm -rf $NETWORK_PATH/ifcfg-${device}.bak
    	echo -e "\033[33m已删除备份的配置文件：$NETWORK_PATH/ifcfg-${device}.bak\033[0m"
    fi
    echo -e "\033[32m-------修改后的网卡配置信息-------\033[0m"
    cat $NETWORK_PATH/ifcfg-$device
    exit 0
fi
if [ -f $NETWORK_PATH/ifcfg-${device}.bak ];then
	mv $NETWORK_PATH/ifcfg-${device}.bak $NETWORK_PATH/ifcfg-$device
	echo -e "\033[33m已恢复之前的配置文件：$NETWORK_PATH/ifcfg-${device}.bak\033[0m"
fi
systemctl restart network
echo "IP地址更改失败，恢复配置！"

# 脚本结束后显示感谢信息
echo "感谢使用咕噜云网卡修改脚本"
