本文主要是介绍shell脚本之一键部署dns正向解析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
shell脚本之一键部署dns正向解析
一、DNS域名系统基础及图文详解请看 DNS域名解析服务及正向解析实验
二、shell一键部署DNS正向解析
#!/bin/bash
#先配置好yum
echo -e "----正在验证当前为仅主机还是NAT模式----"
ping -c 2 -w 2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "检测当前为NAT模式,为您配置在线yum源"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "在线源已配置完成"
else
echo -e "检测当前为仅主机模式,为您配置本地yum源"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "检测当前为仅主机模式,但光盘未挂载!"
else
echo -e "本地yum源已配置完成"
fi
fi
#配置正向解析
#安装bind软件包
cd ~
yum -y install bind
read -p "请输入想要解析的域名,譬如(www.baidu.com): " a
b=`echo $a | awk -F '.' 'BEGIN{OFS="."}{$2=$2;print $2,$3}'`
c=`ifconfig | grep "inet" | awk 'NR==1 {print $2}'`
#修改主配置文件
sed -i 's/127.0.0.1/any/' /etc/named.conf
sed -i 's/localhost/any/' /etc/named.conf
sed -i '14d' /etc/named.conf
#修改区域配置文件
sed -i '18,50d' /etc/named.rfc1912.zones
sed -i 's/localhost.localdomain/'$b'/' /etc/named.rfc1912.zones
sed -i 's/named.localhost/'$b'.zone/' /etc/named.rfc1912.zones
#修改区域数据配置文件
#备份区域数据配置文件
cd /var/named
cp -p named.localhost $b.zone
sed -i 's/@/'$b'./2' /var/named/$b.zone
sed -i 's/rname.invalid./admin.abc.com./' /var/named/$b.zone
sed -i '8s/@/'$b'./' /var/named/$b.zone
sed -i 's/127.0.0.1/'$c'/' /var/named/$b.zone
sed -i '10c www IN A '$c'' /var/named/$b.zone
#启动服务,关闭防火墙
systemctl stop firewalld
setenforce 0
systemctl start named
systemctl restart named
#named -u named
#如果服务启动卡住,可以执行下面命令解决
#rndc-confgen -r /dev/urandom -a
#在客户端的域名解析配置文件中添加DNS服务器地址
sed -i '2c nameserver '$c'' /etc/resolv.conf
三、脚本授权
四、执行脚本
这篇关于shell脚本之一键部署dns正向解析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!