服务器初始化
#!/bin/bash ###配置静态IP,ip范围:172.16.10.150~188 GWAY="172.16.20.254" NMASK="255.255.255.0" DNS="114.114.114.114" FILE_PATH="/etc/sysconfig/network-scripts/" DEVICE="`ip a |grep -E "^2:"|awk -F ': ' '{print $2}'`" NAME="`ip a |grep -E "^2:"|awk -F ': ' '{print $2}'`" YUM="yum install -y" FILE=ifcfg-$NAME echo $FILE change_IP() { cd $FILE_PATH \cp $FILLE ${FILE}.bak if [ ! -d $FILE_BAK ];then mkdir -p $FILE_BAK fi while true do echo -e "\033[47;36m --------------------------------------------------------------\033[0m" read -p "please Enter IP:Example 172.16.10.188,But,the ip range is 172.16.10.150~188:" IP echo $IP|grep -wE "^([0-9]{1,3}\.){3}[0-9]{1,3}$" if [ $? -eq 0 ];then IP1=`echo $IP|awk -F. '{print $1}'` IP2=`echo $IP|awk -F. '{print $2}'` IP3=`echo $IP|awk -F. '{print $3}'` IP4=`echo $IP|awk -F. '{print $4}'` if [ $IP1 -gt 0 -a $IP1 -lt 255 -a $IP2 -ge 0 -a $IP2 -le 255 -a $IP3 -ge 0 -a $IP3 -le 255 -a $IP4 -ge 0 -a $IP4 -le 255 ];then ping -c 3 $IP >>/dev/null if [ $? -ne 0 ];then echo $IP break else echo "The $IP already config Server,Please retry other IPaddr." fi fi fi done grep "dhcp" $FILE if [ $? -eq 0 ];then sed -i 's/dhcp/static/g' $FILE cat >> $FILE <<EOF IPADDR=$IP NETMASK=$NMASK GATEWAY=$GWAY DNS=$DNS EOF echo "------------------------------------" read -p "Please Ensure Restart Network Service. Yes or No ?" INPUT if [ $INPUT == "yes" -o $INPUT == "Y" -o $INPUT == "y" ];then if [ `rpm -q centos-release|cut -d- -f3` == 7 ];then systemctl restart network elif [ `rpm -q centos-release|cut -d- -f3` == 6 ];then /etc/init.d/network restart fi cat $FILE else exit 0 fi else cd $FILE_PATH read -p "The $FILE IP is Static,Are you sure change server IP:yes or no?" INPUT if [ $INPUT == "yes" ];then sed -i 's/IPADDR=.*/IPADDR=$IP/g' $FILE sed -i 's/NETMASK=.*/NETMASK=$NMASK/g' $FILE sed -i 's/GATEWAY=.*/GATEWAY=$GWAY/g' $FILE echo "DNS=$DNS" >> $FILE read -p "Please Ensure Restart Network Service. Yes or No ?" INPUT if [ $INPUT == "yes" -o $INPUT == "Y" -o $INPUT == "y" ];then if [ `rpm -q centos-release|cut -d- -f3` == 7 ];then systemctl restart network elif [ `rpm -q centos-release|cut -d- -f3` == 6 ];then /etc/init.d/network restart fi cat $FILE fi fi fi }