hostname_ip_set.sh (Centos6)
1、自动判断IP是否合法
2、根据子网掩码位数自动计算网络掩码
#!/bin/bash
#Program:
# 修改网卡、修改IP地址
#History:
# 楚枫默寒 2024/05/10
#定义的一些常量
HOST_NAME='/etc/sysconfig/network'
HOSTNAME=`hostname`
ETH0='/etc/sysconfig/network-scripts/ifcfg-eth0'
RULES='/etc/udev/rules.d/70-persistent-net.rules'
NET_HOSTNAME='/etc/hosts'
#克隆虚拟机后,获取MAC地址
A_MAC_eth0=`cat /etc/udev/rules.d/70-persistent-net.rules | grep --color=auto 'eth0' | cut -d '"' -f 8`
A_MAC_Current=`ifconfig | grep --color=auto 'HWaddr' | cut -b 39-55`
#将MAC地址中的字母替换为小写
MAC_eth0=`tr '[A-Z]' '[a-z]' <<<"$A_MAC_eth0"`
MAC_Current=`tr '[A-Z]' '[a-z]' <<<"$A_MAC_Current"`
#打印获取到的MAC地址
echo "(1) Comparing MAC addresses"
echo $MAC_eth0
echo $MAC_Current
#检查MAC地址是否需要修改
if [ $MAC_eth0 == $MAC_Current ]; then
echo "MAC address does not need to be modified!"
elif [ $MAC_eth0 != $MAC_Current ]; then
echo "Please modify the MAC address to: ${MAC-Current}!"
echo "The areas that need to be modified include ${ETH0} and the MAC address of eth0 in ${RULES}!"
#修改ifcfg-eth0中的MAC地址
echo "Modify the MAC address in ifcfg eth0 to${MAC_Current}..."
sed -i "2c HWADDR=${MAC_Current}" ${ETH0}
#修改后的ifcfg-eth0
cat ${ETH0} | grep --color=auto "HWADDR"
echo "MAC address modification completed in ifcfg eth0!"
#修改70-persistent-net.rules文件,将eth0的MAC地址改为eth1的
#查看70-persistent-net.rules的信息
#cat ${RULES} | grep --color=auto "eth0"
echo "modify 70-persistent-net.rules file"
sed -i '8,$c SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'${MAC_Current}'", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"' ${RULES}
#cat ${RULES} | grep --color=auto "eth0"
fi
#修改IP地址
#首先读取ifcfg-eth0文件
echo "${ETH0}The content is as follows:"
cat ${ETH0}
#修改IP地址
function check_ip() {
local IP=$1
VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
if [ $VALID_CHECK == "yes" ]; then
return 0
else
echo "IP $IP not available!"
return 1
fi
else
echo "IP format error!"
return 1
fi
}
# 函数:计算子网掩码
# 参数:IP地址, 子网掩码的位数
calculate_subnet_mask() {
local IP=$1
local MASK_BITS=24
# 计算掩码的整数表示
local MASK_INT=0
for i in $(seq 1 $MASK_BITS); do
MASK_INT=$(($MASK_INT + 2**(32-$i)))
done
# 将掩码整数转换为点分十进制格式
local MASK_DOTTED=$(printf "%d.%d.%d.%d" \
$(($MASK_INT >> 24 & 255)) \
$(($MASK_INT >> 16 & 255)) \
$(($MASK_INT >> 8 & 255)) \
$(($MASK_INT & 255)))
echo $MASK_DOTTED
}
while true; do
read -p "Enter IP address:" IP
check_ip $IP
read -p "Enter GATEWAY address:" GATE
check_ip $GATE
[ $? -eq 0 ] && break
done
MASKIP=$(calculate_subnet_mask $IP)
if [ ${IP} ]&&[ ${MASKIP} ]&&[ ${GATE} ];then
N_IPADDR=`grep -R 'IPADDR=' ${ETH0}`
N_NETMASK=`grep -R 'NETMASK=' ${ETH0}`
N_GATEWAY=`grep -R 'GATEWAY=' ${ETH0}`
sed -i "s@"$N_IPADDR"@IPADDR="$IP"@g" ${ETH0}
sed -i "s@"$N_NETMASK"@NETMASK="$MASKIP"@g" ${ETH0}
sed -i "s@"$N_GATEWAY"@GATEWAY="$GATE"@g" ${ETH0}
echo "IP: $IP MASKIP: $MASKIP GATEWAY: $GATE"
#重启网卡
service network restart
else
echo "IP is error"
fi
#cat ${ETH0}
read -p "Network configuration completed,Do you want to restart(y/n):" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "About to restart the system..."
reboot
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "After modifying the MAC address,Please manually restart the system..."
fi
#修改主机名
read -p "The current host name is${HOSTNAME},Do you want to modify it(y/n):" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
read -p "Please enter the host name:" hdp
sed -i '/HOSTNAME=/d' ${HOST_NAME}
echo "HOSTNAME="${hdp} >> ${HOST_NAME}
sed -i "/$HOSTNAME/d" ${NET_HOSTNAME}
echo ${IP}" "${hdp} >> ${NET_HOSTNAME}
read -p "Host name modification completed,Do you want to restart(y/n):" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "About to restart the system..."
reboot
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "After modifying the host name, Please manually restart the system..."
fi
fi