shell构建dhcp

Posted zhiyuan-yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell构建dhcp相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#挂载yum仓库

read -p "请输入你yum仓库的挂载点: " dir
echo "正在挂载光盘..."
mount | grep -q "/dev/sr0" && umount /dev/sr0
[ -d $dir ] || mkdir $dir
mount /dev/sr0 $dir &> /dev/null

echo "正在修改repo文件..."
cd /etc/yum.repos.d/
[ -d bak ] || mkdir bak
mv *.repo bak

cat << END > local.repo
[xxx]
name=xxx
baseurl=file://$dir
enabled=1
gpgcheck=0
END

echo "正在清除yum缓存..."
yum clean all &> /dev/null

echo "正在建立yum缓存..."
yum makecache &> /dev/null

[ -d $dir/Packages ] && echo "yum构建成功"

#安装DHCP
echo "正在检测本机是否安装dhcp..."
if [ $(rpm -q dhcp ) = "dhcp-4.2.5-58.el7.centos.x86_64" ] &> /dev/null
then echo "dhcp已经安装"
else
echo "将要安装dhcp"
yum -y install dhcp &> /dev/null
if [ $(rpm -q dhcp ) = "dhcp-4.2.5-58.el7.centos.x86_64" ] &> /dev/null
then echo "dhcp安装完成 "
fi
fi

#修改主配置文件

echo "修改主配置文件,请根据提示输入相应内容"
read -p "请输入你要解析的域名(例如crushlinux.com): " abc
read -p "请输入你要分配的网段(例如192.168.12):" ip
read -p "请输入你要分配的ip范围的起止(2-255)(例100 200)):" begin end
read -p "请输入分配的默认时间和最大时间(单位s)(如 600 7200):" lesstime maxtime

cat << END >/etc/dhcp/dhcpd.conf
option domain-name "$abc";
option domain-name-servers 202.106.0.20,8.8.8.8;

default-lease-time $lesstime;
max-lease-time $maxtime;


subnet $ip.0 netmask 255.255.255.0
range $ip.$begin $ip.$end;
option routers 192.168.12.2;

END
#启动DHCP 设置开机自启
systemctl start dhcpd
systemctl restart dhcpd &> /dev/null
systemctl enable dhcpd &> /dev/null
systemctl status dhcpd | grep running &> /dev/null && echo "dhcp服务已启动"

以上是关于shell构建dhcp的主要内容,如果未能解决你的问题,请参考以下文章

构建DHCP服务及原理,设置专门的网址

中小型企业网络构建之DHCP

linux 构建DHCP服务器

DHCP服务器的构建

构建DHCP服务器

小型公司网络构建——单臂路由与DHCP的结合