这篇文章给大家分享的是有关Shell脚本中如何实现DNS主从同步脚本的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
DNS主从同步脚本实例
PS:两个服务器起好后最好两个服务都重启一下
主服务器配置
#!/bin/bash#DNS主从同步——主服务器rpm -q bindif [ $? -ne 0 ];then yum install bind -y systemctl start namedfi#判断DNS服务是否安装read -p "请输入主服务器地址:" zhuread -p "请输入从服务器地址:" congread -p "域名(请带后缀):" yuquanju="/etc/named.conf"quyu="/etc/named.rfc1912.zones"[ ! -e $quanju.bak ] && cp $quanju $quanju.baksed -i -e "13s/127.0.0.1/$zhu/" -e '21s/localhost/any/' $quanju[ ! -e $quyu.bak ] && cp $quyu $quyu.bak#备份全局、区域配置文件grep "$yu" $quyu &> /dev/nullwhile [ $? -ne 0 ];do sed -i "18azone \"$yu\" IN {\n type master\;\n file \"$yu.dns\"\;\n allow- transfer { $cong\; }\;\n}\;\n" $quyu peizhi="/var/named/$yu.dns"cp -p /var/named/named.localhost $peizhidone#判断域名是否存在,如果不存在进行添加sed -i -e '2d' -e "1a@ IN SOA "$yu"\. admin\."$yu"\. (" /var/named/$yu.dnssed -i '8d' /var/named/$yu.dnssed -i '$d' /var/named/$yu.dnssed -i -e "7a\ NS $yu." -e "/A/s/127.0.0.1/$zhu/" /var/named/$yu.dns#修改区域文件while [ 1 -ge 0 ];doread -p "请输入自定义指针(例:www IN A 8.8.8.8)" zhen echo $zhen >> /var/named/$yu.dns read -p "是否继续添加(y/n)" aif [ $a != "y" ];then breakfidone#循环systemctl stop firewalld.servicesetenforce 0systemctl restart namedjieguo=`systemctl status named | grep "active" &>/dev/null`if [ $? -eq 0 ];then echo "服务启动"else echo "启动失败"fi#判断服务是否能正常启动sed -i -e '3d' -e "2anameserver $zhu" /etc/resolv.conf#用于自检
从服务器配置
#!/bin/bash#DNS主从同步——从服务器rpm -q bindif [ $? -ne 0 ];then yum install bind -yfi#判断DNS服务是否安装read -p "请输入主服务器地址:" zhuread -p "请输入从服务器地址:" congread -p "域名(请带后缀):" yuquanju="/etc/named.conf"quyu="/etc/named.rfc1912.zones"[ ! -e $quanju.bak ] && cp $quanju $quanju.baksed -i -e "13s/127.0.0.1/$cong/" -e '21s/localhost/any/' $quanju[ ! -e $quyu.bak ] && cp $quyu $quyu.bak#备份全局、区域配置文件grep "$yu" $quyu &> /dev/nullwhile [ $? -ne 0 ];do sed -i "18azone \"$yu\" IN {\n type slave\;\n file \"slaves\/$yu.dns\"\;\n masters { $zhu\; }\;\n}\;\n" $quyudone#判断域名是否存在,如果不存在进行添加systemctl restart namedsystemctl stop firewalld.servicesetenforce 0jieguo=`systemctl status named | grep "active" &>/dev/null`if [ $? -eq 0 ];then echo "服务启动"else echo "启动失败"fi#判断服务是否能正常启动
感谢各位的阅读!关于“Shell脚本中如何实现DNS主从同步脚本”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!