CentOSも記述したので、こちらも同様に修正。
修正内容もCentOSのスクリプトと同様で、hostlistのキーとなる値をUUIDからシリアルナンバーに切り替えただけだ。
そのため、ここではスクリプトだけを記載する。
●setip.sh
#!/bin/sh
function get_network()
{
local NETWORK=""
local BROADCAST=""
local NETMASK=""
local IPADDR="$1"
local MASKBIT="$2"
local ip_array=(`echo "$IPADDR" | sed -e "s/\./ /g"`)
local ip_index=0
while [ $ip_index -lt 4 ]
do
if [ "$MASKBIT" -lt 8 ]
then
MASK=$((256-2**$((8-MASKBIT))))
MASK_REV=$((~MASK%256+256))
MASKBIT=0
else
MASK=255
MASK_REV=0
MASKBIT=$((MASKBIT - 8))
fi
NETWORK="${NETWORK}.$((ip_array[ip_index] & MASK))"
BROADCAST="${BROADCAST}.$((ip_array[ip_index] | MASK_REV))"
NETMASK="${NETMASK}.$MASK"
ip_index=$((ip_index + 1))
done
NETWORK=`echo $NETWORK | sed -e "s/^\.//"`
BROADCAST=`echo $BROADCAST | sed -e "s/^\.//"`
NETMASK=`echo $NETMASK | sed -e "s/^\.//"`
echo "$NETWORK $BROADCAST $NETMASK"
}
LIST="./host.list"
NIC0="eth0"
NIC1="eth1"
SCRIPT_DIR=`dirname $0`
cd $SCRIPT_DIR
MYSERIAL_CD=`dmidecode -t system | grep Serial | awk 'BEGIN { FS=": ";} { print $2}'`
IFCFGNIC0="ifcfg-${NIC0}"
IFCFGNIC1="ifcfg-${NIC1}"
if [ -z `cat $LIST | grep "$MYSERIAL_CD"` ]; then
echo "It did not exist in [$ LIST] within [$ MYSERIAL_CD]"
echo ""
exit 1
fi
#Host Name
HOSTNAME=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $1 }'`
#Default Gateway
GATEWAY=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $3 }' | awk 'BEGIN {FS="/"} {print $1}'`
#NIC0
NIC0_IPADDR=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $4 }' | awk 'BEGIN {FS="/"} {print $1}'`
NIC0_PREFIX=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $4 }' | awk 'BEGIN {FS="/"} {print $2}'`
NIC0_HWADDR=`ifconfig $NIC0 | grep -e HWaddr -e ハードウェアアドレス | awk '{print $5}'`
NIC0_NEINFO=(`get_network "$NIC0_IPADDR" "$NIC0_PREFIX"`)
NIC0_NEADDR=${NIC0_NEINFO[0]}
NIC0_BRADDR=${NIC0_NEINFO[1]}
NIC0_PRMASK=${NIC0_NEINFO[2]}
#NIC1
NIC1_IPADDR=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $5 }' | awk 'BEGIN {FS="/"} {print $1}'`
NIC1_PREFIX=`cat $LIST | grep "$MYSERIAL_CD" | awk 'BEGIN {FS=","} { print $5 }' | awk 'BEGIN {FS="/"} {print $2}'`
NIC1_HWADDR=`ifconfig $NIC1 | grep HWaddr -e ハードウェアアドレス | awk '{print $5}'`
NIC1_NEINFO=(`get_network "$NIC1_IPADDR" "$NIC1_PREFIX"`)
NIC1_NEADDR=${NIC1_NEINFO[0]}
NIC1_BRADDR=${NIC1_NEINFO[1]}
NIC1_PRMASK=${NIC1_NEINFO[2]}
#create ifcfg-*
cd /etc/network/
echo "# The loopback network interface" > interfaces
echo "auto lo" >> interfaces
echo "iface lo inet loopback" >> interfaces
echo "" >> interfaces
#ifcfg-NIC0
echo "# The primary network interface" >> interfaces
echo "auto $NIC0" >> interfaces
echo "iface $NIC0 inet static" >> interfaces
echo "address $NIC0_IPADDR" >> interfaces
echo "network $NIC0_NEADDR" >> interfaces
echo "netmask $NIC0_PRMASK" >> interfaces
echo "broadcast $NIC0_BRADDR" >> interfaces
echo "gateway $GATEWAY" >> interfaces
echo "" >> interfaces
#ifcfg-NIC1
echo "# The secondery network interface" >> interfaces
echo "auto $NIC1" >> interfaces
echo "iface $NIC1 inet static" >> interfaces
echo "address $NIC1_IPADDR" >> interfaces
echo "network $NIC1_NEADDR" >> interfaces
echo "netmask $NIC1_PRMASK" >> interfaces
echo "broadcast $NIC1_BRADDR" >> interfaces
echo "gateway $GATEWAY" >> interfaces
echo "" >> interfaces
#set hostname
echo "$HOSTNAME" > /etc/hostname
sed -i -e "/^127.0.1.1/c\127.0.1.1 $HOSTNAME" /etc/hosts
#Network Restart
service network-manager stop
service network-manager start
#Reboot
reboot