Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

Microsoft DNS Server

This is actually really ooooold shit I am posting now but hey I figure now is as good a time as any to get these out.

Anybody that knows me will also know that I hate Windows servers. I like efficiency, power, flexibility and the ability to automate virtually anything.

Microsoft DNS Server is just one of those things that really frustrates me. They seem to expect you to have to do everything using their GUI. They don't provide you with an API but at least they provide you with a separate optional add-on command line interface as part of the Windows 2000 Service Pack 4 Support Tools and Windows XP SP2 Support Tools for Advanced Users (nice they think I'm an "advanced" user - probably all *nix people are relative to Windows norms) called dnscmd. For example:

dnscmd /ZoneAdd mydomain.example.org /Primary /load

This is not so bad as now one can write some scripts to make use of this. This is obviously still not nearly as clean as a proper API though.

Anyway, on the machine that I was working on, the DNS database files were located at %systemroot%\system32\dns.

Each domain seems to have its own file. This is good.

My dilemma was this. I had a list of about 20 domains I needed to import. Each domain's records would be identical (at least, for a start). There were quite a few A, CNAME and MX records.

Firstly, I create myself a "template" file. All I did was to simply set up the first domain. I then went to the above-mentioned directory and got the right file. This I will use as a "base".

If I would do this again, I would naturally write Python scripts. However, at the time I was still a bit of a Python noob, so I decided to write some PHP shell scripts, which is also fair enough I guess as this is pretty simply stuff.

I simply create a text file with all my new domains, each domain on a new line, for example:

mydomain1.example.org
mydomain2.example.org
mydomain3.example.org

Of course my file was like much larger.

Anyway, so then I wrote a small PHP script (actually, I don't even think this qualifies as a "script", just as a quick-and-dirty hack).

<?php

$filename = "template.txt";
$handle = fopen($filename, "r");
$template = fread($handle, filesize($filename));
fclose($handle);

$domains = file("domains.txt");
$cleaned = fopen("domains.txt", "w");

foreach ($domains as $domain) {
    $domain = strtolower(trim($domain));
    if ($domain) {
        fwrite($cleaned, "$domain\n");
        $zonefile = str_replace("mytemplate.example.org", $domain, $template);
        $handle = fopen("output/$domain.dns", "w");
        fwrite($handle, $zonefile);
        fclose($handle);
        echo "$domain zonefile created\n";
    }
}

fclose($cleaned);

As you can see my template file is called template.txt and my list of domains is saved in domains.txt, all in the current working directory. Here the mytemplate.example.org would be the first domain you set up for the template. You'll probably want to change that. Note this is for the file contents, has nothing to do with the file name!

Also note the cleaning the above script does on domains.txt. This is because I copied and pasted the list of domains from an e-mail and it turned out a bit messy.

So just create a directory in your current working directory called output (or whatever) and execute this as a shell script. The easiest is probably php import.php (or whatever you called the file). Otherwise you can do the proper shell script thing (with the #! and chmod +x).

Anyway, now you have all your new zone files created. I did all of this on my Linux box so just transferred all those files in the output directory back to the Windows server.

However, even though the right files are now in %systemroot%\system32\dns, you still need to get Microsoft DNS Server to recognise them. This is not as simple as it should. Being a *nix geek, I immediately restarted the DNS Server background process through using Windows "services". Didn't help... I now need to still add each domain using the new zone files manually.

At that time I didn't know about the command line tool so I did this using the GUI. Really frustrating. Otherwise I would have had another script to show off now. I would have had to run this on the Windows box (never tried to get the PHP command line interface to work on Windows yet). Otherwise I would have been able to use the example dnscmd command earlier in the post to add the individual domains easily and thereby fully automating (well, at least almost-fully-automating) the process.

I'm still trying to figure out what the purpose of this post is, but I guess at the end of the day, I just want to vent my frustration with applications that provide a GUI and no API or command line tools. They just make the life of a coder so much easier. I can't stand repetitive tasks.

The obvious question you have is probably "Did you really save time?" Maybe a little, maybe not, but at the end of the day at least everything worked first touch because the amount of room for human error has been reduced and I had a lot more fun than otherwise. And I also got to do some fun research.

0 Comments

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.