#!/bin/bash
#
# 1、检查当前用户的身份是否有权限
# 2、检查是否有YUM源,如果没有需要创建YUM源的配置文件
# 3、安装DHCP,如果已经安装了,我们可以先删除再安装,但删除之间需要备份原来的dhcpd.conf
# 4、配置dhcp服务,并且启动服务和设置开机自启动
export yum_path=/etc/yum.repos.d/
config_file=/etc/dhcp/dhcpd.conf
#1、检查当前用户的身份是否有权限
[ $UID -ne 0 ] && echo "你没root的权限" && exit 1
# 2、检查是否有YUM源,如果没有需要创建YUM源的配置文件
# 3、安装DHCP,如果已经安装了,我们可以先删除再安装,但删除之间需要备份原来的dhcpd.conf
rpm -q dhcp &> /dev/null
rel1=$?
if [ $rel1 -eq 0 ]
then
cp -a /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.`date +%F-%T`
rpm -e dhcp && echo "dhcp配置文件已备份"
yum -y install dhcp &> /dev/null
rpm -q dhcp &> /dev/null
rel2=$?
if [ rel2 -eq 0 ]
then
echo "dhcp软件已经正确安装"
else
echo "yum源有问题,没有正确安装,等待修复..."
mkdir -p /$yum_path/bak
mv $yum_path/*.repo $yum_path/bak
./yum_config.sh
yum -y install dhcp && echo "dhcp软件已经安装成功"
fi
else
yum -y install dhcp &> /dev/null
rpm -e dhcp &> /dev/null
rel2=&?
if [ $rel2 -eq 0 ]
then
echo "dhcp软件已经正确安装"
else
echo "yum源有问题,没有正确安装,等待修复..."
mkdir -p /$yum_path/bak
mv $yum_path/*.repo $yum_path/bak
./yum_config.sh
yum -y install dhcp && echo "dhcp软件已经安装成功"
fi
fi
# 4、配置dhcp服务,并且启动服务和设置开机自启动
read -p "输入你的DHCP中域名:" domain
read -p "输入你的DHCP中DNS:" dns
read -p "输入你的DHCP作用域:" subnet
read -p "输入你的DHCP网关:" gateway
read -p "输入你的DHCP子网掩码:" mask
read -p "输入你的DHCP地址池的起始位:" start
read -p "输入你的DHCP结束位:" stop
cat > /etc/dhcp/dhcpd.conf <<END
dns-update-style interim;
ignore client-updates;
option domain-name "$domain";
option domain-name-servers $dns;
option time-offset -18000;
default-lease-time 21600;
max-lease-time 43200;
subnet $subnet netmask $mask {
option routers $gateway;
option subnet-mask $mask;
range dynamic-bootp $start $stop;
}
END
service dhcpd start &> /dev/null && echo "dhcpd已经启动了" && chkconfig dhcpd on
原文地址:http://yzg784534398.blog.51cto.com/9719002/1655578