# purpose: make a sample dns for RedHat like system # author: netman
簡單建立 dns 的 shell script
# date: 2003-12-12 # version: v0.6
#-- CAVEATE --# # 1) script has been tested only on RedHat 8.x/9.0 platform. # 2) needs root privilege to run. # 3) only one forward zone and one reverse zone will be created. # if no specified zone names are given, script will assume to # use the current DNS domain(using text.cxm instead if not found) for # forward zone's name, and the Class-C ipv4 subnet of current IP for # reverse zone's name. # 4) the script can NOT determine whether you have the proper delegation, # you should manually delete ANY non-authorized zone settings. # 5) only followint Resource Record will be create by this script: # * SOA (both forward & reverse) # * NS (both forward & reverse) # * MX (forward only) # * A (forward only) # * CNAME (www & ftp, forward only) # * PTR (reverse only) # 6) a backup for named.conf and db directory will be made to /root/backup. # backup will be performed once during the first running only, # unless -b options is given. # 7) absolutely NO WARRANTY while running this script.
#-- CHANGE LOG --# # 1) 2003-12-05 v0.1 by netman # * first version. # 2) 2003-12-06 v0.2 by netman # * improve options selection, and add: # -f, -r, -n # 3) 2003-12-06 v0.3 by netman # * improve name server hostname determination # 4) 2003-12-07 v0.4 by netman # * re-organize options dtermination, and add: # -c, -d, -e # 5) 2003-12-10 v0.5 by netman # * bug-fixed to solve: # - ip alias address: have primary only # - no dns domain: set to test.cxm # - no hostname: set to ns1 # 6) 2003-12-12 v0.6 by netman # * bug-fixed to solve: # - backup dir detection
function get_zone { echo echo "Which name you would like to assign to the $1 zone? " echo "(press Enter for '$2', or press 'n' for none): " read z_name echo $z_name | grep -q ' ' && { echo "Error: no space allowed in zone name." echo " Press ctrl-c to abort or type again:" get_zone $1 $2 } if [ -z "$z_name" ]; then z_name=$2 elif [ "$z_name" = "n" ]; then z_name= fi }
function run_intact { get_zone forward $fwd_zone fwd_zone=${z_name%.} get_zone reverse $rev_zone rev_zone=${z_name%.} echo echo "Give the FQDN of your name-server" echo "(or press Enter for system defaults '$ns_host'): " read _ns_host ns_host=${_ns_host:-$ns_host} }
#-- prepare target dir --# function pre_dir { for dir in $@; do if [ -e $dir -a ! -d $dir ]; then echo "${0##*/}: Error: $dir existed but is not a directory." exit 1 else mkdir -p $dir || { echo "${0##*/}: Error: Can't create dir: $dir !" exit 1 } fi done }
#-- test permission --# for target in $named_conf $db_dir $bak_dir; do [ -e $target ] || continue [ -w $target ] || { echo "${0##*/}: Error: you have no write perssion to $target" exit 2 } done
#-- make backup --# function run_bak { [ -e $source -a -d $bak_dir ] && { cp -a $source $bak_dir || { echo "${0##*/}: Error:can't make backup for $source " exit 3 } } } function do_backup { for source in $named_conf $db_dir; do [ "$backup" = true ] && op=';' || op='||' eval test -e $bak_dir/${source##*/} $op run_bak done }
#-- create default settings if missing --# function create_raw {
test -e $named_conf || { echo "${0##*/}: WARNING: $named_conf seems missing!" echo -n "Do you want me to create it for you? (y/N): " read YN echo $YN | grep -Eq 'Y|y' || return 0
cat > $named_conf <<END // generated by ${0##*/} on $(date)
options { directory "$db_dir"; }; zone "." IN { type hint; file "named.ca"; };
zone "localhost" IN { type master; file "localhost.zone"; allow-update { none; }; };
zone "0.0.127.in-addr.arpa" IN { type master; file "named.local"; allow-update { none; }; };
END
test -e $db_dir/named.ca || { cat > $db_dir/named.ca <<END ; This file holds the information on root name servers needed to ; initialize cache of Internet domain name servers ; (e.g. reference this file in the "cache . " ; configuration file of BIND domain name servers). ; ; This file is made available by InterNIC ; under anonymous FTP as ; file /domain/named.cache ; on server FTP.INTERNIC.NET ; ; last update: Nov 5, 2002 ; related version of root zone: 2002110501 ; ; . 3600000 IN NS A.ROOT-SERVERS.NET. A.ROOT-SERVERS.NET. 3600000 A 198.41.0.4 . 3600000 NS B.ROOT-SERVERS.NET. B.ROOT-SERVERS.NET. 3600000 A 128.9.0.107 . 3600000 NS C.ROOT-SERVERS.NET. C.ROOT-SERVERS.NET. 3600000 A 192.33.4.12 . 3600000 NS D.ROOT-SERVERS.NET. D.ROOT-SERVERS.NET. 3600000 A 128.8.10.90 . 3600000 NS E.ROOT-SERVERS.NET. E.ROOT-SERVERS.NET. 3600000 A 192.203.230.10 . 3600000 NS F.ROOT-SERVERS.NET. F.ROOT-SERVERS.NET. 3600000 A 192.5.5.241 . 3600000 NS G.ROOT-SERVERS.NET. G.ROOT-SERVERS.NET. 3600000 A 192.112.36.4 . 3600000 NS H.ROOT-SERVERS.NET. H.ROOT-SERVERS.NET. 3600000 A 128.63.2.53 . 3600000 NS I.ROOT-SERVERS.NET. I.ROOT-SERVERS.NET. 3600000 A 192.36.148.17 . 3600000 NS J.ROOT-SERVERS.NET. J.ROOT-SERVERS.NET. 3600000 A 192.58.128.30 . 3600000 NS K.ROOT-SERVERS.NET. K.ROOT-SERVERS.NET. 3600000 A 193.0.14.129 . 3600000 NS L.ROOT-SERVERS.NET. L.ROOT-SERVERS.NET. 3600000 A 198.32.64.12 . 3600000 NS M.ROOT-SERVERS.NET. M.ROOT-SERVERS.NET. 3600000 A 202.12.27.33 ; End of File END }
test -e $db_dir/localhost.zone || { cat > $db_dir/localhost.zone <<END \$TTL 86400 \$ORIGIN localhost. @ 1D IN SOA @ root ( 42 ; serial (d. adams) 3H ; refresh 15M ; retry 1W ; expiry 1D ) ; minimum
1D IN NS @ 1D IN A 127.0.0.1
END } test -e $db_dir/named.local || { cat > $db_dir/named.local <<END \$TTL 86400 @ IN SOA localhost. root.localhost. ( 1997022700 ; Serial 28800 ; Refresh 14400 ; Retry 3600000 ; Expire 86400 ) ; Minimum IN NS localhost.
1 IN PTR localhost.
END }
} # end of first test } # end of function
#-- check named.conf --# function check_conf { war_msg="${0##*/}: WARNING: \n\t$named_conf seems up-to-date. Nothing will be done. " adv_msg="If you really want to continue, you can:" for zone in $@; do [ -e $named_conf ] && grep -Eq "$zone" $named_conf && { echo -e "$war_msg" echo "$adv_msg" echo "1) delete '$fwd_zone' & '$rev_zone' sections from $named_conf." echo " OR:" echo "2) run '${0##*/} -u' to override db files in $db_dir." echo -e " Note: this option will not modify $named_conf." exit 4 } done }
#-- modify named.conf --# function mod_conf { for zone in $@; do [ "$to_file" ] && { echo echo "Content will be written to $named_conf :" echo "----start-------------------------------------" } cat >> ${to_file:-$named_conf} <<END zone "$zone" IN { type master; file "$zone"; };
diff 結果(v0.1 - v0.3): 66,7c6,7 < # date: 2003-12-06 < # version: v0.3 --- >; # date: 2003-12-05 >; # version: v.0.1 32c32 < # 1) 2003-12-05 v0.1 by netman --- >; # 1) 2003-12-05 v0.01 by netman 34,37d33 < # 2) 2003-12-06 v0.2 by netman < # * improve options selection < # 3) 2003-12-06 v0.3 by netman < # * improve name server hostname determination 39,43c35 < < interact= < to_file= < backup= < update= --- >; options=hrstbf 48a41,43 >; to_file= >; backup= >; update= 57d51 < ns_host=$(hostname -s).$fwd_zone 66,69d59 < echo "Usage:" < echo -e "\t${0##*/} [-h]" < echo -en "\t${0##*/} [-g|-t] [-u] [-b] [-i|" < echo "[-f fwd_zone] [-r rev_zone] [-n name_server]]" 72,76c62,63 < echo -e "\t-g\trun the script" < echo -e "\t-i\tspecify own names interactively" < echo -e "\t-f\tname of forward zone" < echo -e "\t-r\tname of reverse zone" < echo -e "\t-n\tname of name-server" --- >; echo -e "\t-r\trun the script with default values" >; echo -e "\t-s\tspecify your own zone names" 78d64 < echo -e "\t-u\tforce update db(s)" 79a66 >; echo -e "\t-f\tforce update db(s)" 81,82c68 < echo -e "\t# $0 -tu" < echo -e "\t# $0 -g -f test.cxm -r 3.2.1.in-addr.arpa -n ns.test.cxm" --- >; echo -e "\troot_shell# $0 -tu" 103c89 < [ $# -eq 0 ] && { --- >; echo $@ | grep -q "[^$options-]" || [ $# -eq 0 ] && { 105c91 < echo "${0##*/}: Error: missing argument." --- >; echo "${0##*/}: missing argument or invalid options." 112c98 < echo "(press Enter for '$2', or press 'n' for none): " --- >; echo "(or press Enter for none): " 117c103 < get_zone $1 $2 --- >; get_zone $1 119,123d104 < if [ -z "$z_name" ]; then < z_name=$2 < elif [ "$z_name" = "n" ]; then < z_name= < fi 126,127c107,108 < function run_intact { < get_zone forward $fwd_zone --- >; function run_spec { >; get_zone forward 129c110 < get_zone reverse $rev_zone --- >; get_zone reverse 133,135c114,115 < echo "(or press Enter for system defaults '$ns_host'): " < read _ns_host < ns_host=${_ns_host:-$ns_host} --- >; echo "(or press Enter for system defaults): " >; read ns_host 293c273 < echo "1) delete '$fwd_zone' & '$rev_zone' sections from $named_conf." --- >; echo "1) delete '$zone' sections from $named_conf." 295c275 < echo "2) run '${0##*/} -u' to override db files in $db_dir." --- >; echo "2) run '${0##*/} -f' to override db files in $db_dir." 306,308c286 < echo < echo "Content will be written to $named_conf :" < echo "----start-------------------------------------" --- >; echo "---->; Content will be written to $named_conf :" 317,319d294 < [ "$to_file" ] && { < echo "----end---------------------------------------" < } 324a300 >; ns_host=${ns_host:-$(hostname -s).$fwd_zone} 326d301 < echo $ns_host | grep -q '\.' || ns_host=$ns_host.$fwd_zone 329,331c304 < echo < echo "Content will be written to $db_dir/$fwd_zone :" < echo "----start-------------------------------------" --- >; echo "---->; Content will be written to $db_dir/$fwd_zone :" 349,351d321 < [ "$to_file" ] && { < echo "----end---------------------------------------" < } 356,358c326 < echo < echo "Content will be written to $db_dir/$rev_zone :" < echo "----start-------------------------------------" --- >; echo "---->; Content will be written to $db_dir/$rev_zone :" 373,375d340 < [ "$to_file" ] && { < echo "----end---------------------------------------" < } 381c346 < while getopts ":hgif:r:n:tub" opt; do --- >; while getopts ":$options" opt; do 384,388c349,350 < g) : ;; < f) fwd_zone=$OPTARG ;; < r) rev_zone=$OPTARG ;; < n) ns_host=$OPTARG ;; < i) interact=true ;; --- >; r) : ;; >; s) run_spec ;; 390d351 < u) update=true ;; 391a353 >; f) update=true ;; 398,399c360,361 < [ "$interact" = true ] && run_intact < [ "$update" = true ] || { check_conf $fwd_zone $rev_zone; mod_conf $fwd_zone $rev_zone; } --- >; >; test "$update" = true || { check_conf $fwd_zone $rev_zone; mod_conf $fwd_zone $rev_zone; } 401c363 < [ "$to_file" ] || { --- >; test "$to_file" || {
#-- CHANGE LOG --# # 1) 2003-12-05 v0.1 by netman # * first version. # 2) 2003-12-06 v0.2 by netman # * improve options selection, and add: # -f, -r, -n # 3) 2003-12-06 v0.3 by netman # * improve name server hostname determination # 4) 2003-12-07 v0.4 by netman # * re-organize options dtermination, and add: # -c, -d, -e # 5) 2003-12-10 v0.5 by netman # * bug-fixed to solve: # - ip alias address: have primary only # - no dns domain: set to test.cxm # - no hostname: set to ns1 # 6) 2003-12-12 v0.6 by netman # * bug-fixed to solve: # - backup dir detection