Sei sulla pagina 1di 7

#!

/bin/ksh
##########################################################################
## ##
## DNS SERVER CONFIGURATION SCRIPT FOR TEACH TCP/IP COURSES ##
## Write by ##
## Mr. UDOMSAK NAKSENA ( udomsak.n@g-able.com ) ##
## Sun Instructor Authorize Training Certification ##
## Version 3 support Solaris 10 Zone ##
##########################################################################
DBDIR=/var/named
CFGDIR=/etc
########## Information for MASTER SERVER ########
info () {
echo "\nConfigure path [ /etc ] : \c"
read CFGDIR ; CFGDIR=${CFGDIR:="/etc"}
echo "Database path [ /var/named ] : \c"
read DBDIR ; DBDIR=${DBDIR:="/var/named"}
echo "Server name [ `uname -n` ] : \c"
read name ; name=${name:=`uname -n`}
IP=`/usr/sbin/ifconfig -a | grep inet | grep -v "127.0.0.1" | head -1 |
awk '{print $2}'`
A=`echo $IP | cut -d. -f1`
B=`echo $IP | cut -d. -f2`
C=`echo $IP | cut -d. -f3`
D=`echo $IP | cut -d. -f4`
echo "Server IP address [ $IP ] : \c"
read ip ; ip=${ip:=$IP}
DOMAIN="$D.com"
echo "Domain name [ $DOMAIN ] : \c"
read domain ; domain=${domain:=$DOMAIN}
echo " "
echo "Server name is : $name.$domain"
echo "Server IP is : $ip"
echo "configure file is : $CFGDIR/named.conf"
echo "Database path is : $DBDIR"
echo "\n***** Old configure file will remove to $CFGDIR/named.conf.prev
*****"
echo "***** Old database file will remove to $DBDIR/prev.d *****"
}
########## Infomation for slave SERVER ########
info_slave () {
if [ "$merge" != "y" ]
then
echo "\nConfigure path [ /etc ] : \c"
read CFGDIR ; CFGDIR=${CFGDIR:="/etc"}
echo "Database path [ /var/named ] : \c"
read DBDIR ; DBDIR=${DBDIR:="/var/named"}
fi
echo "Primary Server name : \c"
read name
echo "Primary Server IP address : \c"
read master_ip
echo "Primary Domain name : \c"
read domain
echo " "
echo "Primary Server name is : $name.$domain"
echo "Primary Server IP is : $master_ip"
echo "configure file is : $CFGDIR/named.conf"
echo "Database path is : $DBDIR"
}
########## BIND Configure file ########
namedconf () {
[ -f ${CFGDIR}/named.conf ] && mv ${CFGDIR}/named.conf ${CFGDIR}/named.conf.pre
v
cat > $CFGDIR/named.conf <<EOT
//BIND configure file for $domain
options {
DIRECTORY "$DBDIR" ;
};
zone "." in {
type hint ;
file "named.root" ;
};
zone "$domain" in {
type master ;
file "domain.$domain";
};
zone "in-addr.arpa" in {
type master ;
file "rdomain.$domain";
};
zone "0.0.127.in-addr.arpa" in {
type master ;
file "loopback";
};
/*
zone "slave_domain.com." in {
type slave ;
file "backup-domain";
masters {
0.0.0.0 ;
};
};
zone "$C.$B.$A.in-addr.arpa" in {
type slave ;
file "backup-rdomain";
masters {
0.0.0.0 ;
};
};
*/
EOT
}
########## Create named.root ########
named_root () {
[ -f $DBDIR/named.root ] && mkdir $DBDIR/prev.d 2>&- && \
mv $DBDIR/named.root $DBDIR/prev.d
echo "Enter root name server and IP type Enter to finish"
cat > $DBDIR/named.root <<-EOT
EOT
root="begin"
until [ "$root" = '' ]
do
echo "Root server name eg. A.root-server.net : \c"
read rootname
if [ "$rootname" = '' ]
then break
fi
echo "Root server IP address eg. 192.168.100.253 : \c"
read rootip
cat >> $DBDIR/named.root <<EOT
\$TTL 0h
. in ns $rootname.
$rootname. in a $rootip
EOT
done
}
########## Create Forward domain ########
forward_domain () {
[ -f $DBDIR/domain.$domain ] && mv $DBDIR/domain.$domain $DBDIR/prev.d
cat > $DBDIR/domain.$domain <<EOT
; Forward map for $domain
\$TTL 1h
@ in soa $name.$domain. root.$name.$domain. (
$serial
43200
3600
604800
86400 )
in ns $name.$domain.
; in ns SLAVE.$domain.
in mx 10 mail.$domain.
$name in a $ip
localhost in a 127.0.0.1
;SLAVE in a 0.0.0.0
;Cname MAP
mail in cname $name
www in cname $name
ftp in cname $name
;Client MAP
EOT
}
########## Create Reverse domain ########
reverse_domain () {
[ -f $DBDIR/rdomain.$domain ] && mv $DBDIR/rdomain.$domain $DBDIR/prev.d
cat > $DBDIR/rdomain.$domain <<EOT
; Reverse map for in-addr.arpa.
\$TTL 1h
@ in soa $name.$domain. root.$name.$domain. (
$serial
43200
3600
604800
86400 )
in ns $name.$domain.
; in ns SLAVE.$domain.
$D.$C.$B.$A in ptr $name.$domain.
;0.0.0.0 in ptr SLAVE.$domain.
EOT
}
########## Create loopback domain ########
loopback_domain () {
[ -f $DBDIR/loopback ] && mv $DBDIR/loopback $DBDIR/prev.d
cat > $DBDIR/loopback <<EOT
; Reverse map for 0.0.127.in-addr.arpa.
\$TTL 1h
@ in soa $name.$domain. root.$name.$domain. (
$serial
43200
3600
604800
86400 )
in ns `uname -n`.$domain.
1 in ptr localhost.$domain.
EOT
}
########## /etc/resolv.conf ###########
client_resolv () {
[ -f $CFGDIR/resolv.conf ] && mv $CFGDIR/resolv.conf $CFGDIR/resolv.conf.prev
cat > $CFGDIR/resolv.conf <<EOT
domain $domain
nameserver $ip
EOT
cp $CFGDIR/nsswitch.dns $CFGDIR/nsswitch.conf 2>&-
}
########## Add client to server ###########
client () {
if [ -z "$domain" ]
then
defaultdomain="`/usr/sbin/ifconfig -a | grep inet | \
grep -v "127.0.0.1" | head -1 | awk '{print $2}'`.com"
echo "Domain name [ $defaultdomain ] : \c"
read domain ; domain=${domain:=$defaultdomain}
fi
echo "Enter client name and IP address ENTER to finish"
while :
do
echo "\nClient name : \c" ; read cname
if [ "$cname" = '' ]
then break
fi
echo "Client IP : \c" ; read cip
reverse_cip=`echo $cip | awk -F. '{print $4"."$3"."$2"."$1}'`
echo "$cname in a $cip"
echo "Are you sure to add client to $domain [ y ] : \c "
read addnow
addnow=${addnow:=y}
if [ "$addnow" = 'y' ]
then
cat >> $DBDIR/domain.$domain <<-EOT
$cname in a $cip
EOT
cat >> $DBDIR/rdomain.$domain <<-EOT
$reverse_cip IN PTR $cname.$DOMAIN.
EOT
else
continue
fi
done
}

########## BIND Configure file local domain ########


localdomain () {
cat > $CFGDIR/named.conf <<EOT
//BIND configure file for $domain slave
options {
DIRECTORY "$DBDIR" ;
};
zone "." in {
type hint ;
file "named.root" ;
};
zone "0.0.127.in-addr.arpa" in {
type master ;
file "loopback";
};
EOT
}
########## BIND Configure file for slave local host ########
slave_forward () {
cat >> $CFGDIR/named.conf <<EOT
zone "$domain." in {
type slave ;
file "backup-$domain";
masters {
$master_ip ;
};
};
EOT
}
########## BIND Configure file for slave reverse ########
slave_reverse () {
cat >> $CFGDIR/named.conf <<EOT
zone "in-addr.arpa" in {
type slave ;
file "backup-reverse-$domain";
masters {
$master_ip;
};
};
EOT
}
####### yes no loop ######
info_loop () {
ans=n
until [ "$ans" = "y" ]
do
$1
echo "\nAre you sure [y] : \c"
read ans ; ans=${ans:=y}
done
}
########## Display menu and select choice ########
while :
do
clear
echo "\t\tDNS Server Configuration\n"
echo "\t1. Primary Server Configuration"
echo "\t2. Secondary Server Configuration"
echo "\t3. Add client to Server"
echo "\t4. exit\n"
echo "Select [1] : \c"
read choice ; choice=${choice:="1"}
case $choice in
1) info_loop info
[ -d $CFGDIR ] || mkdir -p $CFGDIR
[ -d $DBDIR ] || mkdir -p $DBDIR
serial=`date +%Y%m%d`
namedconf
named_root
forward_domain
reverse_domain
loopback_domain
client
client_resolv
;;
2)
if [ -f $CFGDIR/named.conf ]
then
echo "Configure slave merge to current server [
y ] : \c"
read merge ; merge=${merge:=y}
if [ "$merge" = "y" ]
then
info_loop info_slave
slave_forward
slave_reverse
else
mv $CFGDIR/named.conf $CFGDIR/named.conf
.prev
mkdir $DBDIR/prev.d 2>&-
mv $DBDIR/* $DBDIR/prev.d 2>&-
info_loop info_slave
localdomain
slave_forward
slave_reverse
named_root
loopback_domain
fi
continue
fi
[ -d $CFGDIR ] || mkdir -p $CFGDIR
[ -d $DBDIR ] || mkdir -p $DBDIR
info_loop info_slave
localdomain
slave_forward
slave_reverse
named_root
loopback_domain
;;
3) client ;;
4) exit 0 ;;
*) continue ;;
esac
done

##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.jsp
##############################################################################

Potrebbero piacerti anche