```bash
#!/bin/bash
printCountOf() {
for i in `seq 1 $1`;do echo -n "#";done
}
printFormat() {
ALL_CHAR_COUNT=120
INPUT_CHAR_COUNT=$(echo -n $* | wc -m)
if [ $ALL_CHAR_COUNT -gt $INPUT_CHAR_COUNT ];then
SEPATATOR_COUNT=$((($ALL_CHAR_COUNT-$INPUT_CHAR_COUNT)/2))
printCountOf $SEPATATOR_COUNT
echo -n "- "
echo -n $*
echo -n " -"
printCountOf $SEPATATOR_COUNT
if [ $((($ALL_CHAR_COUNT-$INPUT_CHAR_COUNT)%2)) -ne 0 ];then
echo -n "#"
fi
echo
else
echo $*
fi
}
setHostname() {
hostname $1
echo "$1" > /etc/hostname
echo -e "\033[32m"
printFormat HostName Setting Success : [/etc/hostname] effected : content replaced by [$1]
echo -e "\033[0m"
}
setIfcfg() {
IFCFG_COUNT=$(ls /etc/sysconfig/network-scripts/ | grep ifcfg- | grep -v ifcfg-lo | wc -l)
if [ $IFCFG_COUNT -eq 1 ];then
IFCFG_FILE=/etc/sysconfig/network-scripts/$(ls /etc/sysconfig/network-scripts/ | grep ifcfg- | grep -v ifcfg-lo)
if [ $(cat $IFCFG_FILE | grep ^BOOTPROTO= | wc -l) -ge 1 ];then
sed -i '/BOOTPROTO/cBOOTPROTO=static' $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : BOOTPROTO=static
else
echo "BOOTPROTO=static" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : BOOTPROTO=static
fi
if [ $(cat $IFCFG_FILE | grep ^ONBOOT= | wc -l) -ge 1 ];then
sed -i '/ONBOOT/cONBOOT=yes' $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : ONBOOT=yes
else
echo "ONBOOT=yes" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : ONBOOT=yes
fi
if [ $(cat $IFCFG_FILE | grep ^IPADDR= | wc -l) -ge 1 ];then
sed -i "/IPADDR/cIPADDR=$1" $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : IPADDR=$1
else
echo "IPADDR=$1" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : IPADDR=$1
fi
if [ $(cat $IFCFG_FILE | grep ^NETMASK= | wc -l) -ge 1 ];then
sed -i "/NETMASK/cNETMASK=$2" $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : NETMASK=$2
else
echo "NETMASK=$2" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : NETMASK=$2
fi
if [ $(cat $IFCFG_FILE | grep ^GATEWAY= | wc -l) -ge 1 ];then
sed -i "/GATEWAY/cGATEWAY=$3" $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : GATEWAY=$3
else
echo "GATEWAY=$3" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : GATEWAY=$3
fi
if [ $(cat $IFCFG_FILE | grep ^DNS1= | wc -l) -ge 1 ];then
sed -i "/DNS1/cGATEWAY=$4" $IFCFG_FILE
printFormat $IFCFG_FILE : Modify : DNS1=$4
else
echo "DNS1=$4" >> $IFCFG_FILE
printFormat $IFCFG_FILE : Add : DNS1=$4
fi
echo -e "\033[32m"
printFormat Network Setting Success
echo -e "\033[0m"
elif [ $IFCFG_COUNT -lt 1 ];then
echo -e "\033[31m"
printFormat Network Setting Fail : No Ifcfg File to config
echo -e "\033[0m"
else
echo -e "\033[31m"
printFormat Network Setting Fail : Ifcfg File Too Many, Can not Choose
echo -e "\033[0m"
fi
}
read -p "Please Set Hostname (Enter Skip Setting) : " INPUT_HOSTNAME
if [ -z $INPUT_HOSTNAME ];then
echo -e "\033[32m您已经跳过了主机名设置!\033[0m"
fi
while [ -z $INPUT_IP_ADDRESS ]
do
read -p "Please Set Ip Address : " INPUT_IP_ADDRESS
if [[ $INPUT_IP_ADDRESS =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]];then
echo -e "\033[32m输入内容合法\033[0m"
else
INPUT_IP_ADDRESS=""
echo -e "\033[31m输入内容不合法,请重新输入!\033[0m"
fi
done
while [ -z $INPUT_NETMASK ]
do
read -p "Please Set NETMASK (255.255.255.0) : " INPUT_NETMASK
if [[ $INPUT_NETMASK =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]];then
echo -e "\033[32m输入内容合法\033[0m"
else
INPUT_NETMASK=""
echo -e "\033[31m输入内容不合法,请重新输入!\033[0m"
fi
done
while [ -z $INPUT_GATEWAY ]
do
read -p "Please Set GATEWAY : " INPUT_GATEWAY
if [[ $INPUT_GATEWAY =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]];then
echo -e "\033[32m输入内容合法\033[0m"
else
INPUT_GATEWAY=""
echo -e "\033[31m输入内容不合法,请重新输入!\033[0m"
fi
done
while [ -z $INPUT_DNS ]
do
read -p "Please Set DNS : " INPUT_DNS
if [[ $INPUT_DNS =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]];then
echo -e "\033[32m输入内容合法\033[0m"
else
INPUT_DNS=""
echo -e "\033[31m输入内容不合法,请重新输入!\033[0m"
fi
done
if [ -n $INPUT_HOSTNAME ];then
setHostname $INPUT_HOSTNAME
fi
setIfcfg $INPUT_IP_ADDRESS $INPUT_NETMASK $INPUT_GATEWAY $INPUT_DNS
echo -e "\033[32mservice network restart # 重启网络\033[0m"
```