os_release_file="/etc/os-release"  
OS="" 
HOSTNAME=`hostname`
#设置ip存在的文件
IP_FILE="/opt/ip.list"
SetIpByCentOS(){
	#private_ip=`ifconfig eth0 | grep 172 | awk '{print $2}'`
	#echo $private_ip
	echo "start set ip by centos ..." >> /var/log/set_ip.log
	if [ -f $IP_FILE  ];then
        	echo "FILE EXIST" >> /var/log/set_ip.log
        	yum install -y NetworkManager
       		service NetworkManager start
        	for i in `cat $IP_FILE`;do nmcli con mod "System eth1" +ipv4.addresses $i;done
        	service network restart
        	sleep 3
		CallBackFunc "SUCCESS"
		#rm /var/log/set_ip.log
		rm /opt/ip.list
	else
        	echo "NO FIlE" >> /var/log/set_ip.log
        	CallBackFunc "ERROR: NO IP FILE!"
	fi
}
SetIpByUbuntu(){
       echo "start set ip by ubuntu ..." >> /var/log/set_ip.log
        if [ -f $IP_FILE  ];then
                echo "FILE EXIST" >> /var/log/set_ip.log
                apt install -y network-manager
                service NetworkManager start
                for i in `cat $IP_FILE`;do nmcli con mod "System eth1" +ipv4.addresses $i;done
                netplan -r apply
                sleep 3
                CallBackFunc "SUCCESS"
                #rm /var/log/set_ip.log
                rm /opt/ip.list
        else
                echo "NO FIlE" >> /var/log/set_ip.log
                CallBackFunc "ERROR: NO IP FILE!"
        fi

}
#SetIpByDebian(){
#}

CallBackFunc(){
#ip配置后回调下，确认完成
	curl -X POST -u noc:iIfxa1uACIXYz -d "{'host':${HOSTNAME},'msg':${1}}" https://registry.ows-docker.cloud/alert
}
if [ -f "$os_release_file" ]; then  
    source "$os_release_file"  
  
    if [[ "$ID" == "debian" ]]; then  
        echo "---debian---"	
    elif [[ "$ID" == "centos" ]]; then  
        echo "---centos---" > /var/log/set_ip.log
	SetIpByCentOS
    elif [[ "$ID" == "ubuntu" ]]; then
	echo "---ubuntu---"
	SetIpByUbuntu
    else  
        echo "你的操作系统未被识别 $ID" > /var/log/set_ip.log 
        exit 1	
    fi  
else  
    echo "无法确定操作系统 $ID" > /var/log/set_ip.log
    exit 1
fi


