#!/bin/bash#Auth:Darius#自動化安裝dhcp服務#"$1"為測試IP,用來查看IP段是否能通eno=`ifconfig|awk '{print $1}'|head -1|awk -F ":" '{print $1}'`file=/etc/sysconfig/network-sc ...
#!/bin/bash
#Auth:Darius
#自動化安裝dhcp服務
#"$1"為測試IP,用來查看IP段是否能通
eno=`ifconfig|awk '{print $1}'|head -1|awk -F ":" '{print $1}'`
file=/etc/sysconfig/network-scripts/ifcfg-$eno
file1=/etc/dhcp/dhcpd.conf
dir=/etc/yum.repos.d
#ip地址設置
ping -c 4 -i 0.1 -W 2 $1 &>/dev/null
if [ $? -ne 0 ];then
echo -e "DEVICE=$eno\nTYPE=Ethernet\nONBOOT=yes\nBOOTPROTO=static" >$file
read -p "請輸入ip地址": ip
read -p "請輸入子網掩碼": nt
read -p "請輸入網關": gw
echo "IPADDR=$ip" >> $file
echo "NETMASK=$nt" >>$file
echo "GATEWAY=$gw" >>$file
if [ $? -eq 0 ]
then
[ $? -eq 0 ] && echo "IP地址配置成功"
else
echo "IP地址配置失敗"
fi
else
echo "網路測試成功"
fi
#構建yum本地倉庫
mkdir /media/cdrom
mount /dev/sr0 /media/cdrom/
#設置光碟自動掛載
echo "/dev/sr0 /media/cdrom iso9660 defaults 0 0" >> /etc/fstab
mkdir $dir/bak
mv $dir/C* $dir/bak/
echo -e "[yum]\nname=yum\nbaseurl=file:///media/cdrom\ngpgcheck=0\nenabled=1" >$dir/yum.repo
yum clean all &>/dev/null
yum makecache &>/dev/null
echo "yum倉庫構建完成"
#檢測並安裝dhcp服務
yum install -y dhcp &>/dev/null
echo "dhcp服務安裝成功"
cat /usr/share/doc/dhcp*/dhcpd.conf.sample >$file1
[ $? -eq 0 ] && echo "模板複製成功"
cp $file1 /etc/dhcp/dhcpd.conf.bak
echo -e "subnet 192.168.1.0 netmask 255.255.255.0{\nrange 192.168.1.2 192.168.1.100;\noption routers 192.168.1.1;\n}" >>$file1
echo "文件配置完成"
service dhcpd start &>/dev/null
chkconfig dhcpd on &>/dev/null
[ $? -eq 0 ] && echo "dhcp服務已啟動"