#!/bin/bash
# Linux克隆后配置修改,修改如下内容
# 主机名
# 设备映射
# 修改网卡
# 配置网关
# 配置NTP
# AUTHOR : ldk
# VERSION: 1.0
# 日期 :2016.01.06
unset V_HOSTNAME
unset V_IPADDR
unset V_NETMASK
unset V_GATEWAY
unset V_NTPADDR
# 输入新主机名
while true
do
read -p "########### Please input new hostname: " V_HOSTNAME
if [ ! -n "$V_HOSTNAME" ]; then
echo "New hostname Input error! "
else
if [[ $V_HOSTNAME =~ ^[a-zA-Z0-9]{1,30}[a-zA-Z0-9_-]{0,30}[a-zA-Z0-9]{0,30}$ ]] && [[ $V_HOSTNAME =~ [a-zA-Z] ]];then
break
else
echo "New hostname Input error! "
fi
fi
done
# 输入新IP地址
while true
do
read -p "########### Please input new ip address: " V_IPADDR
if [ ! -n "$V_IPADDR" ]; then
echo "New ip address Input error! "
else
if [[ $V_IPADDR =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
V_TEMPIP=($V_IPADDR)
IFS=$OIFS
if [[ ${V_TEMPIP[0]} -lt 255 && ${V_TEMPIP[1]} -lt 255 && ${V_TEMPIP[2]} -lt 255 && ${V_TEMPIP[3]} -lt 255 && ${V_TEMPIP[0]} -ne 0 && ${V_TEMPIP[3]} -ne 0 ]]; then
break
else
echo "New ip address Input error! "
fi
else
echo "New ip address Input error! "
fi
fi
done
# 输入新IP掩码
while true
do
read -p "########### Please input new ip netmask: " V_NETMASK
if [ ! -n "$V_NETMASK" ]; then
echo "New ip netmask Input error! "
else
if [[ $V_NETMASK =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
V_TEMPMASK=($V_NETMASK)
IFS=$OIFS
if [[ ${V_TEMPMASK[0]} -lt 256 && ${V_TEMPMASK[1]} -lt 256 && ${V_TEMPMASK[2]} -lt 256 && ${V_TEMPMASK[3]} -lt 256 ]]; then
V_SUMMASK=`echo $V_NETMASK | awk -F'.' '{print ($1*(2^24)+$2*(2^16)+$3*(2^8)+$4)}'`
V_BINMASK=`echo "obase=2;${V_SUMMASK}" | bc`
if [[ $V_BINMASK =~ ^[1]{1,32}[0]{1,32}$ ]]; then
V_BINMASKLENGTH=${#V_BINMASK}
if [ $V_BINMASKLENGTH -eq 32 ];then
V_PREFIX=`echo $V_BINMASK | awk -F'0' '{print length($1)}'`
break
else
echo "New ip netmask Input error! "
fi
else
echo "New ip netmask Input error! "
fi
else
echo "New ip netmask Input error! "
fi
elif [[ $V_NETMASK =~ ^[0-9]{1,2}$ ]];then
if [[ $V_NETMASK -gt 0 && $V_NETMASK -lt 32 ]];then
V_PREFIX=$V_NETMASK
break
else
echo "New ip netmask Input error! "
fi
else
echo "New ip netmask Input error! "
fi
fi
done
# 输入新网关
while true
do
read -p "########### Please input new gateway: " V_GATEWAY
if [ ! -n "$V_GATEWAY" ]; then
echo "New gateway Input error! "
else
if [[ $V_GATEWAY =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
V_TEMPGW=($V_GATEWAY)
IFS=$OIFS
if [[ ${V_TEMPGW[0]} -lt 255 && ${V_TEMPGW[1]} -lt 255 && ${V_TEMPGW[2]} -lt 255 && ${V_TEMPGW[3]} -lt 255 && ${V_TEMPGW[0]} -ne 0 && ${V_TEMPGW[3]} -ne 0 ]]; then
break
else
echo "New gateway Input error! "
fi
else
echo "New gateway Input error! "
fi
fi
done
# 输入新NTP服务器
while true
do
read -p "########### Please input new ntp server: " V_NTPADDR
if [ ! -n "$V_NTPADDR" ]; then
echo "New ntp server Input null,continue "
break
else
if [[ $V_NTPADDR =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
V_TEMPNTP=($V_NTPADDR)
IFS=$OIFS
if [[ ${V_TEMPNTP[0]} -lt 255 && ${V_TEMPNTP[1]} -lt 255 && ${V_TEMPNTP[2]} -lt 255 && ${V_TEMPNTP[3]} -lt 255 && ${V_TEMPNTP[0]} -ne 0 && ${V_TEMPNTP[3]} -ne 0 ]]; then
break
else
echo "New ntp server Input error! "
fi
else
echo "New ntp server Input error! "
fi
fi
done
echo "##########################################"
echo "## The new hostname is: "$V_HOSTNAME
echo "## The new ip address is: "$V_IPADDR
echo "## The new ip prefix is: "$V_PREFIX
echo "## The new gateway is: "$V_GATEWAY
echo "## The new ntp server is: "$V_NTPADDR
echo "##########################################"
read -p "Please confirm above information (Enter for OK, or CTL+C)" V_CONFIRM
# 修改主机名
echo "######## Begin modify hostname ########"
echo "NETWORKING=yes" > /etc/sysconfig/network
echo "HOSTNAME="$V_HOSTNAME >> /etc/sysconfig/network
echo "GATEWAY="$V_GATEWAY >> /etc/sysconfig/network
hostname $V_HOSTNAME
echo "######## End modify hostname ########"
# 设置NTP服务器
if [ ! -n "$V_NTPADDR" ]; then
:
else
if [ -f /var/spool/cron/root ];then
echo "######## Begin delete old ntp server ########"
vi /var/spool/cron/root <<EOF
/ntpdate
dd
:wq!
EOF
fi
echo "######## Begin add ntp server ########"
echo "0,30 * * * * /usr/sbin/ntpdate "$V_NTPADDR" && /sbin/hwclock --systohc > /dev/null 2>&1 " >> /var/spool/cron/root
echo "######## End add ntp server ########"
fi
# 修改网卡配置
if [ -d /sys/class/net/eth0 ];then
echo "######## Begin change ip config ########"
echo "TYPE=Ethernet" > /etc/sysconfig/network-scripts/tmp-eth0
echo "DEVICE=eth0" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "NAME=eth0" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "HWADDR="`cat /sys/class/net/eth0/address` >> /etc/sysconfig/network-scripts/tmp-eth0
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "IPADDR="$V_IPADDR >> /etc/sysconfig/network-scripts/tmp-eth0
echo "PREFIX="$V_PREFIX >> /etc/sysconfig/network-scripts/tmp-eth0
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/tmp-eth0
mv -f /etc/sysconfig/network-scripts/tmp-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0
echo "Begin to restart network service"
service network restart
echo "######## End change ip config ########"
else
IFNUM=0
ls -d /sys/class/net/eth* | sort -n | awk -F'/' '{print $NF}' | while read IFNAME
do
V_IFMAC=`cat /sys/class/net/$IFNAME/address`
V_OIFMAC=`grep ^"SUBSYSTEM==\"net\"" /etc/udev/rules.d/70-persistent-net.rules | grep eth$IFNUM | awk -F'\"' '{print $8}' `
vi /etc/udev/rules.d/70-persistent-net.rules <<EOF
/$V_IFMAC
dd
:1,$s/$V_OIFMAC/$V_IFMAC/g
:wq!
EOF
if [ -f /etc/sysconfig/network-scripts/ifcfg-eth$IFNUM ];then
mv /etc/sysconfig/network-scripts/ifcfg-eth$IFNUM /etc/sysconfig/ifcfg-eth$IFNUM
fi
if [ $IFNUM -eq 0 ];then
echo "######## Begin change ip config ########"
echo "TYPE=Ethernet" > /etc/sysconfig/network-scripts/tmp-eth0
echo "DEVICE=eth0" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "NAME=eth0" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "HWADDR="`cat /sys/class/net/$IFNAME/address` >> /etc/sysconfig/network-scripts/tmp-eth0
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/tmp-eth0
echo "IPADDR="$V_IPADDR >> /etc/sysconfig/network-scripts/tmp-eth0
echo "PREFIX="$V_PREFIX >> /etc/sysconfig/network-scripts/tmp-eth0
echo "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/tmp-eth0
mv -f /etc/sysconfig/network-scripts/tmp-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0
fi
((IFNUM=IFNUM+1))
done
echo "######## End change ip config ########"
echo "Ethernet name have changed, please reboot the server"
fi
chage -M 60 root