A Bash script to extract a list of domains served by Bind (DNS server)

in #bash7 years ago

Here is a quick and easy procedure to use bash to extract a list of all the domains being serverd by a Bind DNS server.

First, tell Bind to do a dump of all its zones:

$ sudo rndc dumpdb -zones

This will create a file called 'cache_dump.db'. For me, this file was put in the /var/named directory. Your mileage may vary, depending on what distro you are using. For Ubuntu, it should end up in: /var/cache/bind/named_dump.db. You can search for the file either way.

Secondly, extract only the lines which contain the domain names:

$ grep 'Zone dump of*' cache_dump.db > zonelist1.txt

This will give you a list like this:

; Zone dump of 'somedomain.com/IN'
; Zone dump of 'anotherdomain.co.uk/IN'
; Zone dump of 'athirddomain.com/IN'

Third, removed all text outside of the quotes:

$ cut -d "'" -f2 < zonelist1.txt >> zonelist2.txt

This will give you a list like:

somedomain.com/IN
anotherdomain.co.uk/IN
athirddomain.com/IN

Fourth, remove the trailing text... (the '/IN' bits)

$ cat zonelist2.txt | cut -f1 -d"/" >> zonelist3.txt 

This will give you:

somedomain.com
anotherdomain.co.uk
athirddomain.com

Now... there may be a file .misc lines in that text file which are not domains. Eyeball the text file. You may fine a few lines which end in .bind, for example. Simply remove them by hand. There shouldn't be more than a half-dozen or so.

Finally, sort the list:

$ cat zonelist3.txt | sort > zonelist4.txt

And in the end you get a sorted list of all your domains (zones files) covered by your Bind server:

anotherdomain.co.uk
athirddomain.com
somedomain.com
...

Have a look at the output of each of these steps along the way. You may have to slightly tweak one or two of the commands, but these are the basic steps.

Hope this helps!