天天看点

shell脚本实现DNS的自动安装与配置

#!/bin/bash

read -p   "Please enter your zone:"  ZONE

read -p  "Please enter your IPNET,[Usage: 192.168.0.]:" NET

YOURIP=`ifconfig eth0| grep "inet addr" | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'`

LAST=`echo $YOURIP | awk 'BEGIN {FS="."}{print $4}'`

FANXIANG=`echo $NET | awk 'BEGIN {FS="."}{print $3"."$2"."$1}'`

rpm -q bind &>/dev/null && rpm -q  caching-nameserver &>/dev/null

let BOOL=`echo $?`

if [ $BOOL -eq 1 ];  then

    yum -y install bind

    yum -y install caching-nameserver

fi

let BOOL2=`echo $?`

if [ $BOOL2 -eq 1 ]; then

  echo "Sorry,bind is not install!"   

  exit

#********************************************

echo "make named.conf......"

cat>>/etc/named.conf<<EOF

  options {

        directory "/var/named";

};

zone "." IN {

         type hint;

         file "named.ca";

zone "localhost" IN {

        type master;

        file "localhost.zone";

zone "0.0.127.in-addr.arpa"  IN {

         type master;

         file "named.local";

zone "$ZONE" IN {

        file "$ZONE.zone";

zone "$FANXIANG.in-addr.arpa" IN  {

        file "$NET.zone";

EOF

#*******************************************

echo "make $ZONE.zone....."

echo '$TTL 86400' > /var/named/$ZONE.zone

cat>>/var/named/$ZONE.zone<<EOF

@    IN    SOA    $ZONE.   root.linna.com. (

                  2011021501

            1H

            10M

            7D    

            1D )

        IN     NS    ns.$ZONE.

        IN    MX 10    mail.$ZONE.

ns        IN    A    $YOURIP

mail        IN    A    $YOURIP

www        IN    A    $YOURIP

#******************************************

echo "make $NET.zone......"

echo '$TTL 86400' > /var/named/$NET.zone

cat>>/var/named/$NET.zone<<EOF 

                  2011021502

$LAST        IN    PTR    ns.$ZONE.

$LAST        IN    PTR    mail.$ZONE.

$LAST        IN    PTR    www.$ZONE.

service named configtest

echo "Checking........."

chown :named /etc/named.conf

chown :named /var/named/*

#sed -i 's/^nameserver.*$/nameserver $YOURIP/g' /etc/resolv.conf

echo -e "search example.com \n nameserver $YOURIP " > /etc/resolv.conf

echo "OK!Well Down!"

wait

service named start

chkconfig named on

chkconfig named --list

#*****************************************

dig -x $YOURIP

dig -t A $ZONE

#****************************************

本文转自 490999122 51CTO博客,原文链接:http://blog.51cto.com/lyp0909/503605,如需转载请自行联系原作者