Shell脚本实战之DNS主从同步脚本实例
DNS主从同步脚本实例
PS:两个服务器起好后最好两个服务都重启一下
主服务器配置
#!/bin/bash #DNS主从同步――主服务器 rpm -q bind if [ $? -ne 0 ];then yum install bind -y systemctl start named fi #判断DNS服务是否安装 read -p "请输入主服务器地址:" zhu read -p "请输入从服务器地址:" cong read -p "域名(请带后缀):" yu quanju="/etc/named.conf" quyu="/etc/named.rfc1912.zones" [ ! -e $quanju.bak ] && cp $quanju $quanju.bak sed -i -e "13s/127.0.0.1/$zhu/" -e '21s/localhost/any/' $quanju [ ! -e $quyu.bak ] && cp $quyu $quyu.bak #备份全局、区域配置文件 grep "$yu" $quyu &> /dev/null while [ $? -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 $peizhi done #判断域名是否存在,如果不存在进行添加 sed -i -e '2d' -e "1a@ IN SOA "$yu"\. admin\."$yu"\. (" /var/named/$yu.dns sed -i '8d' /var/named/$yu.dns sed -i '$d' /var/named/$yu.dns sed -i -e "7a\ NS $yu." -e "/A/s/127.0.0.1/$zhu/" /var/named/$yu.dns #修改区域文件 while [ 1 -ge 0 ];do read -p "请输入自定义指针(例:www IN A 8.8.8.8)" zhen echo $zhen >> /var/named/$yu.dns read -p "是否继续添加(y/n)" a if [ $a != "y" ];then break fi done #循环 systemctl stop firewalld.service setenforce 0 systemctl restart named jieguo=`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 bind if [ $? -ne 0 ];then yum install bind -y fi #判断DNS服务是否安装 read -p "请输入主服务器地址:" zhu read -p "请输入从服务器地址:" cong read -p "域名(请带后缀):" yu quanju="/etc/named.conf" quyu="/etc/named.rfc1912.zones" [ ! -e $quanju.bak ] && cp $quanju $quanju.bak sed -i -e "13s/127.0.0.1/$cong/" -e '21s/localhost/any/' $quanju [ ! -e $quyu.bak ] && cp $quyu $quyu.bak #备份全局、区域配置文件 grep "$yu" $quyu &> /dev/null while [ $? -ne 0 ];do sed -i "18azone \"$yu\" IN {\n type slave\;\n file \"slaves\/$yu.dns\"\;\n masters { $zhu\; }\;\n}\;\n" $quyu done #判断域名是否存在,如果不存在进行添加 systemctl restart named systemctl stop firewalld.service setenforce 0 jieguo=`systemctl status named | grep "active" &>/dev/null` if [ $? -eq 0 ];then echo "服务启动" else echo "启动失败" fi #判断服务是否能正常启动