lost and found ( for me ? )

BIND: Create zone files automatically from a template file

荒削りだけどこんな感じ。

[ 手順 ]

1. 作成したいゾーンファイルのリストを準備
2. テンプレートファイルを準備
3. スクリプトを実行
4. 動作確認

[ Let’s try step by step ]

1. 作成したいゾーンファイルのリストを準備

書式:  ゾーン名:IPアドレス 
root@ubuntu-7:~/my_work# cat zonelist
example1.com:192.168.0.1
example2.com:192.168.0.2
example3.com:192.168.0.3


2. テンプレートファイルを準備

DOMAIN、IPADDR のところが、上記 zonelist に記載されているドメイン名、IPアドレスに置き換わる。
root@ubuntu-7:~/my_work# cat template.txt
$TTL    3600
@       IN      SOA     DOMAIN. root.DOMAIN.  (
                               2010122401      ; Serial
                               3600            ; Refresh
                               900             ; Retry
                               3600000         ; Expire
                               3600 )          ; Minimum
       IN      NS      ns1.DOMAIN.
       IN      MX      0 mx1.DOMAIN.
ns1     IN      A       IPADDR
mx1     IN      A       IPADDR
www     IN      A       IPADDR


3. スクリプトを実行
root@ubuntu-7:~/my_work# cat create_zone.sh
#!/bin/bash

DATAFILE=zonelist
TEMPLATE=template.txt

for data in $(cat $DATAFILE)
do
       dom=${data%:*}
       ip=${data#*:}
       sed "{ s/DOMAIN/$dom/g; s/IPADDR/$ip/g; }" $TEMPLATE > $dom.zone
done

root@ubuntu-7:~/my_work# ./create_zone.sh

root@ubuntu-7:~/my_work# ls *.zone
example1.com.zone  example2.com.zone  example3.com.zone

root@ubuntu-7:~/my_work# named-checkzone example1.com example1.com.zone
zone example1.com/IN: loaded serial 2010122401
OK

root@ubuntu-7:~/my_work# cat example1.com.zone
$TTL    3600
@       IN      SOA     example1.com. root.example1.com.  (
                               2010122401      ; Serial
                               3600            ; Refresh
                               900             ; Retry
                               3600000         ; Expire
                               3600 )          ; Minimum
       IN      NS      ns1.example1.com.
       IN      MX      0 mx1.example1.com.
ns1     IN      A       192.168.0.1
mx1     IN      A       192.168.0.1
www     IN      A       192.168.0.1


4. 動作確認
root@ubuntu-7:~/my_work# named -v
BIND 9.7.0-P1

root@ubuntu-7:~/my_work# /etc/init.d/bind9 start

root@ubuntu-7:~/my_work# dig @127.1 www.example1.com.

; <<>> DiG 9.7.0-P1 <<>> @127.1 www.example1.com.
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14978
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;www.example1.com.              IN      A

;; ANSWER SECTION:
www.example1.com.       3600    IN      A       192.168.0.1

;; AUTHORITY SECTION:
example1.com.           3600    IN      NS      ns1.example1.com.

;; ADDITIONAL SECTION:
ns1.example1.com.       3600    IN      A       192.168.0.1


that’s it :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.