拓扑图:

代码:
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
#!/bin/bash#2017-12-3#by-duwentaodomain_name="hetian.com"rip=`ifconfigeth0|grepMask|awk-F":"'{print $2}'|awk-F" "'{print $1}'|awk-F"."'{print $3"."$2"."$1}'`ip=`ifconfigeth0|grepMask|awk-F":"'{print $2}'|awk-F" "'{print $1}'`ip_last=`ifconfigeth0|grepMask|awk-F":"'{print $2}'|awk-F" "'{print $1}'|awk-F"."'{print $4}'`named="/etc/named.conf"#主配文件Forward_parsing_file="/var/named/$domain_name.zone"#正向解析文件Reverse_parsing_file="/var/named/$rip.in-addr.arpa.zone"#反向解析文件#check the yum allocation of right and wrongfunctionCheck_Yum( ){yum clean all>/dev/null2>&1#判断yum配置文件的格式是否正确if[ $? -ne'0'];thenecho-e"\033[31m yum allocation is wrong \033[0m"Repair_Yumelse#定义变量用来查看yum有多少安装包Yum_Num=`yum repolist all|greprepolist|awk-F":"'{print $2}'|seds'/,//'g|seds'/ //'g`if[ $Yum_Num -eq0 ];thenecho-e"\033[31m yum allocation is wrong \033[0m"Repair_Yumelseecho-e"\033[32m yum allocation is right \033[0m"fifi}#repair yumfunctionRepair_Yum( ){dvd_file="/etc/yum.repos.d/dvd.repo"read-p"Do you want to repair yum y/n:"checkif["$check"="y"-o"$check"="yes"-o"$check"="Y"];thenmount/dev/cdrom/mnt1>/dev/null2>&1if[ -f $dvd_file ];thenrm-rf $dvd_filetouch$dvd_filecat>> $dvd_file << +END+[dvd]name=dvdbaseurl=file:///mntgpgcheck=0+END+elsetouch$dvd_filecat>> $dvd_file << +END+[dvd]name=dvdbaseurl=file:///mntgpgcheck=0+END+echo-e"\033[31m Please run it again and see the repair results \033[0m"exit0fielif["$check"="n"-o"$check"="N"-o"$check"="no"];thenecho-e"\033[31m yum allocation is wrong,Please configure it manually \033[0m"exit0elsewhile[[ 1 < 2 ]]doecho-e"\033[31m Your input is wrong,please try again \033[0m"Repair_Yumdonefi}#install dns serverfunctionInstall_DNS_Server( ){#把原有的卸载干净yum remove bind* -yrm-rf/var/named/*rm-rf/etc/named.conf.rpm*#开始安全dns服务yuminstallbind bind-libs bind-utils -y/etc/init.d/namedrestart >/dev/null2>&1if[ $? -eq0 ];thenecho-e"\033[32m DNS server install sucessful \033[0m"elseecho-e"\033[31m DNS server install failed try again \033[0m"fi}#Edit named configuration filefunctionEdit_Conf ( ){#修改sed-i'/^include/d'$namedsed-i s'/127.0.0.1/any/'g $namedsed-i s'/localhost/any/'g $namedsed-i'/recursion/i version "I do not tell you";'$named#隐藏版本号sed-i s'/recursion yes/recursion no/'g $named#关闭rescursion功能,防止spoofigsed-i'/recursion/i fetch-glue no;'$named#关闭glue fetching功能##Edit named configuration filecat>> $named << +END+zone"$domain_name"IN{typemaster;file"$Forward_parsing_file";};zone"$rip.in-addr.arpa"IN{typemaster;file"$Reverse_parsing_file";};include"/etc/named.rfc1912.zones";include"/etc/named.root.key";+END+#Edit Forward and Reverse configuration file#Edit Forward configuration fileif[ ! -f $Forward_parsing_file ];thentouch$Forward_parsing_fileelserm-rf $Forward_parsing_filetouch$Forward_parsing_fileficat>> $Forward_parsing_file << +END+\$TTL 1D@ IN SOA $domain_name. root.$domain_name. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimum@ IN NS dns.smile.com.dns IN A $ipmail IN A $ipwww IN A $ip+END+#Edit Reverse configuration fileif[ ! -f $Reverse_parsing_file ];thentouch$Reverse_parsing_fileelserm-rf $Reverse_parsing_filetouch$Reverse_parsing_fileficat>> $Reverse_parsing_file << +END+\$TTL 86400@ IN SOA $rip.in-addr.arpa. root.$domain_name. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimum@ IN NS dns.$domain_name.$ip_last IN PTR www.$domain_name.$ip_last IN PTR mail.$domain_name.+END+#修改权限chgrpnamed $namedchgrpnamed $Reverse_parsing_filechgrpnamed $Forward_parsing_fileecho"nameserver $ip">>/etc/resolv.conf/etc/init.d/namedrestart}#copyfunctioncopy( ){scp-r/root/shell/DNS_Server_masters.sh root@172.24.10.106:/rootif[ $? -ne'0'];thenecho-e"\033[31m 拷贝失败 \033[0m"elseecho-e"\033[32m 拷贝成功 \033[0m"fissh-l root 172.24.10.106'/bin/bash /root/DNS_Server_masters.sh'}#chenge_DNS_slavefunctionchenge_DNS_slave ( ){#修改sed-i'/^include/d'$namedsed-i s'/127.0.0.1/any/'g $namedsed-i s'/localhost/any/'g $namedsed-i'/recursion/i version "I do not tell you";'$named#隐藏版本号sed-i s'/recursion yes/recursion no/'g $named#关闭rescursion功能,防止spoofigsed-i'/recursion/i fetch-glue no;'$named#关闭glue fetching功能##Edit named configuration filecat>> $named << +END+zone"$domain_name"IN{typeslave;file"$Forward_parsing_file";masters {172.24.10.105;};};zone"$rip.in-addr.arpa"IN{typeslave;file"$Reverse_parsing_file";masters {172.24.10.105;};};include"/etc/named.rfc1912.zones";include"/etc/named.root.key";+END+chmodg+w/var/named/echo"nameserver $ip">>/etc/resolv.confservice named restart}#echo menufunctionmenu ( ){echo-e"\033[32m 1.检查yum是否配置正确 \033[0m"echo-e"\033[32m 2.安装DNS服务 \033[0m"echo-e"\033[32m 3.修改DNS-master服务的配置文件 \033[0m"echo-e"\033[32m 4.拷贝文件到目标主机并且进入目标主机\033[0m"echo-e"\033[32m 5.修改目标机的主配文件\033[0m"echo-e"\033[32m exit.输入exit或者ctrl+c退出 \033[0m"echo-e"\033[32m help.请输入1|2|3|help|exit \033[0m"read-p"请选择你菜单:"menucase$menuin1) Check_Yum;;2) Install_DNS_Server;;3)if[ `rpm -qa |grepbind-9.8|wc-l` -ne1 ];thenecho-e"\033[31m 请确保已经安装DNS服务 \033[0m"fiEdit_Conf;;4)copy;;5) chenge_DNS_slave;;help)echo-e"\033[32m help.请输入1|2|3|4 \033[0m"menu;;exit)exit0;;*)echo-e"\033[31m 输入有误,请重新输入 \033[0m"menu;;esac}while[[ 1 < 2 ]]dohostname=`hostname`if["$hostname"="DNS_SERVER_1"];thenecho"当前设备是DNS_SERVER_1:请选择你菜单:"menuelseecho"当前设备是$hostname:请选择你菜单:"menufidone
执行结果:
微信公众号:

本文转自 天道酬勤VIP 51CTO博客,原文链接:http://blog.51cto.com/tdcqvip/2047790