#!/bin/bash
# install dhcp server
if [ -d /media/Server ]; then
if (rpm -q dhcp &> /dev/null) ; then
echo "DHCP Server has been installed , then we will remove it."
rpm -e dhcp --nodeps &> /dev/null
if ( rpm -q dhcp &> /dev/null ); then
echo "Remove the dhcp RPM failly "
exit 2
else
echo "Remove the dhcp RPM is successfull "
fi
rpm -i /media/Server/dhcp*.rpm --nodeps --force &> /dev/null
if ( rpm -q dhcp &> /dev/null ); then
echo "DHCP RPM have been installed successfully."
else
echo "DHCP RPM was installed failly."
exit 2
fi
else
rpm -i /media/Server/dhcp*.rpm --nodeps --force &> /dev/null
if ( rpm -q dhcp &> /dev/null ); then
echo "DHCP RPM have been installed successfully."
else
echo "DHCP RPM was installed failly."
exit 2
fi
fi
else
echo "/dev/cdrom isn't mounted , Pls do it."
exit 1
fi
echo "################## config dhcp server ##################"
read -p "Pls put into the DHCP IP : " ip
read -p "Pls put into the DHCP MASK: " mask
read -p "Pls put into the DHCP GATEWAY: " gw
echo "################## Set IP Address : ###################"
IFCFG="/etc/sysconfig/network-scripts/ifcfg-eth0"
ipnet=$( echo $ip|awk -F. '{print $1"."$2"."$3}')
grep -q "none$" $IFCFG
if [ $? -ne 0 ]; then
sed -i 's/dhcp/none' $IFCFG
cat <<dhcp! >> $IFCFG
NETMASK=$mask
IPADDR=$ip
GATEWAY=$gw
dhcp!
else
sed -i "s/IPADDR=.*/IPADDR=$ip/" $IFCFG
sed -i "s/NETMASK=.*/NETMASK=$mask/" $IFCFG
sed -i "s/GATEWAY=.*/GATEWAY=$gw/" $IFCFG
fi
service network restart &> /dev/null
if (ping -c 2 -i 0.1 -W 1 $ip &> /dev/null); then
echo "IP Address has been configured successfully ."
else
echo "IP Address has been configured failly ."
exit 4
fi
cat /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample > /etc/dhcpd.conf
sed -i "s/192.168.0/$ipnet/g" /etc/dhcpd.conf
sed -i "s/192.168.1.1/8.8.8.8,4.4.4.4/g" /etc/dhcpd.conf
service dhcpd restart &> /dev/null
service dhcpd status &> /dev/null
if [ $? -eq 0 ]; then
echo "DHCP start successfully."
else
echo "DHCP start failly."
fi
搭建dhcp服务器完成
希望大家多多关注breaklinux.com 工作室,有错误的地方请指出,谢谢!
作者:---新
转载于:https://blog.51cto.com/breaklinux/1531320