欢迎访问我的私人网站:https://lixiaonan.xyz
这是一键部署dhcpd服务脚本
vim dhcpd.sh
#!/bin/bash
#------------------------------------------------------------
#作者:李笑男
#联系方式:lxn3642@163.com
#这是一个一键部署DHCP服务的脚本
#操作系统版本为:RHEL7.3
#------------------------------------------------------------
#定义变量
subnet=172.25.1.0
netmask=255.255.255.0
range='172.25.1.100 172.25.1.200'
gateway=172.25.1.2
domain_name_servers=172.25.1.2
domain_name=lixiaonan.xyz
dhcpd_conf=/etc/dhcp/dhcpd.conf
#测试yum源
test_yum(){
yum clean all &> /dev/null
repo=`yum repolist 2> /dev/null | grep repolist | awk '{print $2}'`
if [ "$repo" != "0" ]
then
[ -f $dhcpd_conf ] || yum install dhcp -y &> /dev/null
else
echo 'yum源错误、请先配置yum源!'
exit
fi
}
#备份原文件
backup(){
if [ -f $dhcpd_conf ]
then
mv $dhcpd_conf $dhcpd_conf\.bak
fi
}
#新建配置文件
create_conf(){
cat > $dhcpd_conf << EOF
subnet $subnet netmask $netmask {
range $range ;
option domain-name-servers $domain_name_servers ;
option domain-name "$domain_name";
option routers $gateway ;
#option broadcast-address 10.5.5.31;
default-lease-time 600;
max-lease-time 7200;
}
EOF
}
#调用函数完成DHCP服务的搭建
test_yum
backup
create_conf
systemctl restart dhcpd &> /dev/null && echo 'dhcpd服务启动成功' || echo 'dhcpd服务启动失败'
systemctl enable dhcpd &> /dev/null && echo 'dhcpd服务加入开机自启成功' || echo 'dhcpd服务加入开机自启失败'