shell 一键部署DNS正向解析,用户可自定义域名及IP,可多次执行

#!/bin/bash
#安装bind软件包
yum install -y bind &> /dev/null

#请用户输入要进行域名解析的域名与对应IP,并存放到某个文件以便后面进行调用
read -p "请输入域名(如www.baidu.com): " name
echo $name > /etc/dns-yuming.txt
read -p "请输入对应IP地址:" ip
echo $ip > /etc/dns-ip.txt

#修改 /etc/named.conf 主配置文件
sed -i '13 s/127.0.0.1/any/' /etc/named.conf
sed -i '21 s/localhost/any/' /etc/named.conf
sed -i '14 s/^/#/' /etc/named.conf

#把域名差分成2个变量
name1=`awk -F "." '{print $1}' /etc/dns-yuming.txt`
name23=`awk -F "." '{print $2"."$3}' /etc/dns-yuming.txt`

#修改 /etc/named.rfc1912.zones 区域配置文件
sed -i "24 a zone \"$name23\" IN {
     \n\ttype master;\n\tfile \"$name23\.zone\";\n\tallow-update { none; };\n};\n" /etc/named.rfc1912.zones

#cp -p 复制区域数据配置文件进行修改
cp -p /var/named/named.localhost /var/named/$name23\.zone

sed -i "s/rname.invalid./admin.$name23"."/" /var/named/$name23\.zone
sed -i "8 s/@/$name23"."/" /var/named/$name23\.zone
sed -i "10d" /var/named/$name23\.zone
sed -i "9 s/127.0.0.1/$ip/" /var/named/$name23\.zone
sed -i "9a $name1\tA\t$ip" /var/named/$name23\.zone

#定义DNS服务器IP地址,也就是本机,因机而异
sed -i "1i nameserver 192.168.88.4 " /etc/resolv.conf

#关闭安全限制
systemctl stop firewalld
setenforce 0

#启动服务,restart以便多次执行该脚本时,保证服务重新启动
systemctl start named
systemctl restart named

你可能感兴趣的:(dns,shell)