Shell脚本 - 一键部署正反向解析DNS

#!/bin/bash
#获取IP地址
export getIP=`ifconfig | grep "inet" | awk 'NR==1 {print $2}'`
export getcIP=`cIPUntil`
#检测光盘挂载
function cdrom(){
  df -h | grep "sr0" &> /dev/null
  if [ $? -eq 1 ];then
     mount /dev/cdrom /mnt &> /dev/null
     bind=`ls /mnt/Packages | egrep "^bind-"[0-9]`
     rpm /mnt/Packages/$bind
     main
  else
     main
  fi
}
#检测DNS安装包
function dns(){
  rpm -q bind &> /dev/null
  if [ $? -eq 1 ];then
     yum -y install bind &> /dev/null
     main
  else
     main
  fi
}
#检测网络
function network(){
  local timeout=1
  local target=www.baidu.com
  local status=`curl -I -s --connect-timeout ${timeout} ${target} -w %{http_code} | tail -n1`
  if [ "x$status" = "x200" ]; then
     return 1
  else
     return 0
  fi
  return 0
}
#IP反向解析库
function cIPUntil(){
    array=(`echo $getIP | tr "." " "`)
    length=${#array[@]}
    for ((i=0;i<length/2;i++)){
        backup=${array[$i]}
        array[$i]=${array[$length-$i-1]}
        array[$length-$i-1]=$backup
        unset array[3]
    }
    cIP=`echo ${array[@]} | tr " " "."`
    echo $cIP
}
#主代码
function main(){

    cp -p /etc/named.conf named.conf.bak
    sed -i 's/127.0.0.1/any/g' /etc/named.conf 
    sed -i 's/localhost/any/g' /etc/named.conf
    
    #用户输入
    read -p "请输入域名:" domain
    
    #正向解析
    echo -e "
    zone \"$domain\" IN {
    \ttype master;
    \tfile \"$domain.zone\";
    \tallow-update { none; };
    };
    " >> /etc/named.rfc1912.zones
    cd /var/named
    echo -e "$TTL 1D
@       IN SOA  $domain. admin.$domain. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      $domain.
        A       $getIP
www IN  A       $getIP
    " > $domain.zone
    chown root:named $domain.zone
    
    #反向解析
    array=(`echo $getIP | tr "." " "`)
    last=${array[3]}
    echo -e "
    zone \"$getcIP.in-addr.arpa\" IN {
    \ttype master;
    \tfile \"$domain.local\";
    \tallow-update { none; };
    };
    " >> /etc/named.rfc1912.zones
    cd /var/named
    echo -e "$TTL 1D
@       IN SOA  $domain. admin.$domain. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      $domain.
        A       $getIP
$last IN  A       www.$domain
    " > $domain.zone
    chown root:named $domain.zone
    
    systemctl start named
    echo "反向解析已经部署完毕!"
}
#################执行####################
network
if [ $? -eq 0 ];then
    cdrom
else
    dns
fi

标签: echo, zone, DNS, named, Shell, 正反, array, domain, getIP

相关文章推荐

添加新评论,含*的栏目为必填