分享
 
 
 

[原創] 簡單建立 dns 的 shell script

王朝厨房·作者佚名  2007-01-02
窄屏简体版  字體: |||超大  

常碰到一些朋友抱怨說 linux 的 dns 很難設。

但經我個人觀察,100% 都是打錯字或格式疏忽而已~~~~ ^_^

這裡我寫了隻 shell script ,可以幫助建立簡單的正解及反解檔,

然後將剩下的"簡單"的部份,則自己來修改了...

download:

http://www.study-area.org/linux/src/sample_dns.sh.tgz

初版原始碼:

[code:1:212a63ac08]#!/bin/bash

set -u

# purpose: make a sample dns for RedHat like system

# author: netman<netman@study-area.org>

# date: 2003-12-05

# version: v.0.1

#-- 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.01 by netman

# * first version.

options=hrstbf

named_conf=/etc/named.conf

db_dir=/var/named

bak_dir=/root/backup

wtty=$(ps | grep $$ | tail -n 1 | awk '{print $2}')

to_file=

backup=

update=

#-- get zone info --#

fwd_zone=$(hostname -d)

fwd_zone=${fwd_zone:-test.cxm}

host_if=eth0

host_ip=$(ifconfig | grep -A1 $host_if | awk '/inet/{print $2}' | sed 's/^.*://')

host_ip=${host_ip:-192.168.1.1}

rev_zone="$(echo ${host_ip%.*} | awk -F. '{print $3"."$2"."$1}').in-addr.arpa"

ns_ptr=${host_ip##*.}

serial_nu=$(date +%Y%m%d)01

opt_ttl=86400

function print_usage {

echo "-------------------------------------------------------------"

echo "Script Name:"

echo -e "\t${0##*/}"

echo "Options:"

echo -e "\t-h\tprint this help"

echo -e "\t-r\trun the script with default values"

echo -e "\t-s\tspecify your own zone names"

echo -e "\t-t\ttest only"

echo -e "\t-b\tforce backup"

echo -e "\t-f\tforce update db(s)"

echo "Example:"

echo -e "\troot_shell# $0 -tu"

echo "-------------------------------------------------------------"

exit 0

}

function print_choice {

echo

echo "Please select one:"

echo "h): to print HELP."

echo "t): to TEST the script only."

echo "q): to QUIT."

echo -n 'Your choice? '

read action

case $action in

h|H) print_usage ;;

t|T) exec $0 -t ;;

q|Q) echo; exit 0 ;;

*) print_choice ;;

esac

}

echo $@ | grep -q "[^$options-]" || [ $# -eq 0 ] && {

echo

echo "${0##*/}: missing argument or invalid options."

print_choice

}

function get_zone {

echo

echo "Which name you would like to assign to the $1 zone? "

echo "(or press Enter 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

}

}

function run_spec {

get_zone forward

fwd_zone=${z_name%.}

get_zone reverse

rev_zone=${z_name%.}

echo

echo "Give the FQDN of your name-server"

echo "(or press Enter for system defaults): "

read ns_host

}

#-- prepare backup dir --#

test -d $bak_dir || {

mkdir $bak_dir || {

echo "${0##*/}: Error: Can't create backup dir: $bak_dir "

exit 1

}

}

#-- 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 {

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 . <file>"

; 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 {

for zone in $@; do

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:"

[ -e $named_conf ] && grep -Eq "$zone" $named_conf && {

echo -e "$war_msg"

echo "$adv_msg"

echo "1) delete '$zone' sections from $named_conf."

echo " OR:"

echo "2) run '${0##*/} -f' 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 "----> Content will be written to $named_conf :"

}

cat >> ${to_file:-$named_conf} <<END

zone "$zone" IN {

type master;

file "$zone";

};

END

done

}

#-- modify rr db --#

function mod_rr {

ns_host=${ns_host:-$(hostname -s).$fwd_zone}

ns_host=${ns_host%.}

[ "$fwd_zone" ] && {

[ "$to_file" ] && {

echo "----> Content will be written to $db_dir/$fwd_zone :"

}

cat > ${to_file:-$db_dir/$fwd_zone} <<END

\$TTL $opt_ttl

@ IN SOA $ns_host. root.$ns_host. (

$serial_nu ; Serial

28800 ; Refresh

14400 ; Retry

604800 ; Expire

86400 ) ; Minimum

@ IN NS $ns_host.

${ns_host%%.*} IN A $host_ip

www IN CNAME $ns_host.

ftp IN CNAME $ns_host.

END

}

[ "$rev_zone" ] && {

[ "$to_file" ] && {

echo "----> Content will be written to $db_dir/$rev_zone :"

}

cat > ${to_file:-$db_dir/$rev_zone} <<END

\$TTL $opt_ttl

@ IN SOA $ns_host. root.$ns_host. (

$serial_nu ; Serial

28800 ; Refresh

14400 ; Retry

604800 ; Expire

86400 ) ; Minimum

@ IN NS $ns_host.

$ns_ptr IN PTR $ns_host.

END

} # end of test

} # end of function

#-- main script --#

while getopts ":$options" opt; do

case "$opt" in

h) print_usage ;;

r) : ;;

s) run_spec ;;

t) to_file=/dev/$wtty ;;

b) backup=true ;;

f) update=true ;;

\?) print_usage ;;

esac

done

do_backup

create_raw

test "$update" = true || { check_conf $fwd_zone $rev_zone; mod_conf $fwd_zone $rev_zone; }

mod_rr

test "$to_file" || {

echo "${0##*/}: Okay, all done!"

echo "Don't forget to restart your named daemon and check log messages."

echo "Enjoy!"

}

exit 0[/code:1:212a63ac08]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有