SSDZ – Shell Script for DNS Zones



SSDZ (Shell Script for DNS zones) is a useful shell script that I developed to automatize DNS zone management operations. The main functions:

Download

The archive contains 3 files:
ssdz.sh – Shell Script for DNS zones
named.conf – a named.conf file, for testing purposes. I will use it to show how the script works
dnstemplate.txt – the basic template for zone file

Configuration

Before to use the script, you should perform script configuration. Open the file and alter the basic variables: named.conf location, zones directory and zonefile template location. The defaults are:

namedconf=/home/dima/work/named.conf
dnstemplate=/home/dima/work/dnstemplate.txt
zonesdir=/home/dima/work/zonesdb

If the above variables will be incorrect, the script will generate the errors on run:

dima@gentoo-nb ~/work $ ./ssdz.sh
Path to named.conf is incorrect.
Specify correct path ('namedconf variable'). Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh
Path to zonefile template is incorrect.
Specify correct path ('dnstemplate' variable). Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh
Path to zones directory is incorrect.
Specify correct path ('zonesdir' variable). Exiting...

Normal usage

Examples for normal usage when adding a zone:

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain.com 182.17.33.240
Domain domain.com with ip address 182.17.33.240 added

dima@gentoo-nb ~/work $ cat named.conf |grep domain.com
zone "domain.com" { type master; file "/home/dima/work/zonesdb/domain.com.db"; };

dima@gentoo-nb ~/work $ cat zonesdb/domain.com.db
$TTL 14400
domain.com. IN SOA ns1.domain.com. dnsadm.domain.com. (
2008103000      ; Serial
10800           ; Refresh after 3 hours
3600            ; Retry after 1 hour
2600000         ; Expire after 1 week
1800 )          ; Time To Live

@               IN      NS      ns2.domain.com.
@               IN      NS      ns1.domain.com.
@               IN      MX      10 domain.com.
@               IN      A       182.17.33.240
*               IN      CNAME   domain.com.

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain2.com 182.17.34.240
Domain domain2.com with ip address 182.17.34.240 added

dima@gentoo-nb ~/work $ cat named.conf |grep domain2.com
zone "domain2.com" { type master; file "/home/dima/work/zonesdb/domain2.com.db"; };

dima@gentoo-nb ~/work $ cat zonesdb/domain2.com.db
$TTL 14400
domain2.com. IN SOA ns1.domain2.com. dnsadm.domain2.com. (
2008103000      ; Serial
10800           ; Refresh after 3 hours
3600            ; Retry after 1 hour
2600000         ; Expire after 1 week
1800 )          ; Time To Live

@               IN      NS      ns2.domain2.com.
@               IN      NS      ns1.domain2.com.
@               IN      MX      10 domain2.com.
@               IN      A       182.17.34.240
*               IN      CNAME   domain2.com.

Examples for normal usage when removing a zone:

dima@gentoo-nb ~/work $ cat named.conf |grep domain2.com
zone "domain2.com" { type master; file "/home/dima/work/zonesdb/domain2.com.db"; };

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain2.com 182.17.34.240
/bin/rm: remove regular file `/home/dima/work/zonesdb/domain2.com.db'? y
Domain domain2.com with ip address 182.17.34.240 removed

dima@gentoo-nb ~/work $ cat named.conf |grep domain2.com

dima@gentoo-nb ~/work $ ls -la zonesdb/
total 12
drwxr-xr-x 2 dima dima 4096 2008-10-30 09:39 .
drwxr-xr-x 3 dima dima 4096 2008-10-30 09:39 ..
-rw-r--r-- 1 dima dima  552 2008-10-30 09:36 domain.com.db

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain.com 182.17.33.240
/bin/rm: remove regular file `/home/dima/work/zonesdb/domain.com.db'? y
Domain domain.com with ip address 182.17.33.240 removed

dima@gentoo-nb ~/work $ cat named.conf |grep domain.com

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain.com 182.17.33.240
Domain domain.com with ip address 182.17.33.240 added

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain2.com 182.17.34.240
Domain domain2.com with ip address 182.17.34.240 added

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain2.com 182.17.34.241
IP address for domain domain2.com in zonefile is not 182.17.34.241.
Nothing to remove. Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain3.com 182.17.34.240
Zonefile don't exists. Nothing to remove. Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain2.com 182.17.34.240
/bin/rm: remove regular file `/home/dima/work/zonesdb/domain2.com.db'? y
Domain domain2.com with ip address 182.17.34.240 removed

dima@gentoo-nb ~/work $ ./ssdz.sh domaindrop domain.com 182.17.33.240
/bin/rm: remove regular file `/home/dima/work/zonesdb/domain.com.db'? y
Domain domain.com with ip address 182.17.33.240 removed

The script will not remove the domain if it's name is incorrect. Also, the domain will not be removed if the IP address provided differs from that specified in zonefile.

Error handling

Below are several examples for error handling:

dima@gentoo-nb ~/work $ ./ssdz.sh
Usage: ./ssdz.sh [domainadd|domaindrop]
Missing action. Specify 'domainadd' or 'domaindrop'. Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd
Usage: ./ssdz.sh [domainadd|domaindrop]
Missing domain. Specify domain in form 'domain.com'. Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain.com
Usage: ./ssdz.sh [domainadd|domaindrop]
Missing IP address. Specify correct IP address. Exiting...

dima@gentoo-nb ~/work $ ./ssdz.sh domainadd domain.com 182.17.33.270
Usage: ./ssdz.sh [domainadd|domaindrop]
Invalid IP address. Specify correct IP address. Exiting...