simonas

Well-Known Member
Apr 21, 2013
141
0
16
Lithuania
cPanel Access Level
Root Administrator
Hello,

I wanted to better organize my DNS zones, so i wrote basic program in PHP (i feel relaxed with it and letting run perl script as root could mess up original zones), which will rewrite your .db files.

It's a tested program, but use at your own risk, always make backups!!!

I will keep it updated, and will accept new feature requests.

To use:
copy all .db files from /var/named to some folder
Create folder where you will store new zones, make it writable if on *nix
Update variables in the script to suit your needs, change folders
Run the script
Check if zone files are ok, not corrupted
Upload new zone files to /var/named/
Do service named restart
If it fails do named-checkconf -z /etc/named.conf
to see which zones are bad.

USE at your own risk!
By default program will delete mail autodiscover / autoconfig entries, disable them in tweak settings first
Also it will delete ftp and mail subdomains by default.

PHP:
<?php
/*
 *     Mass DNS zone update program
 *     [email protected]
 *
 */

// If you already changed some zones today, please increase it, keep it with leading zero!
$revision = '01';

// Time to live, used on every record, change as you see fit
$TTL = 1800;
// SOA serial number - consists of todays date and revision number, change as you see fit
$SOA_serial = date('Ymd') . $revision;
// SOA refresh time, change as you see fit
$SOA_refresh = 3600;
// SOA retry, change as you see fit
$SOA_retry = 1800;
// SOA expire, change as you see fit
$SOA_expire = 604800;
// Array records to remove, first words only. Lines starting with following words will be removed!
$remove = array('mail','ftp','webdisk','autoconfig','autodiscover','_autodiscover._tcp');

// Where you store your zone files
$zones = 'C:/Serveris/zones'; // No trailing /
// Where to write new zone files
$write_to = 'C:/Serveris/new'; // No trailing /

$files = scandir($zones);

foreach($files as $filename) {
    if($filename != '.' && $filename != '..') {
        $contents = file($zones . "/$filename", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
        foreach($contents as $nr => $line) {
            $line = trim($line);

            $words = explode("\t", $line);
            if(in_array($words[0], $remove)) {
               unset($contents[$nr]);
            }
            elseif(strpos($line, '$TTL') === 0) {
                $contents[$nr] = '$TTL' . "\t$TTL";
            }
            elseif(preg_match("/IN\s+SOA/", $line)) {
                $contents[$nr] = preg_replace('/\d+\s+IN\s+SOA/', $TTL . "\tIN\tSOA", $line);
            }
            elseif(preg_match("/;\s*serial/i", $line)) {
                    $contents[$nr] = "\t\t\t\t\t\t$SOA_serial\t; Serial Number";
            }
            elseif(preg_match("/;\s*refresh/", $line)) {
                    $contents[$nr] = "\t\t\t\t\t\t$SOA_refresh\t\t; refresh, seconds";
            }
            elseif(preg_match("/;\s*retry/", $line)) {
                    $contents[$nr] = "\t\t\t\t\t\t$SOA_retry\t\t; retry, seconds";
            }
            elseif(preg_match("/;\s*expire/", $line)) {
                    $contents[$nr] = "\t\t\t\t\t\t$SOA_expire\t\t; expire, seconds";
            }
            elseif(preg_match("/\d+\s+\)/", $line)) {
                    $contents[$nr] = "\t\t\t\t\t\t$TTL )\t\t; minimum, seconds";
            }
            elseif(preg_match("/\d+\s+IN\s/", $line)) {
                    $contents[$nr] = preg_replace("/\d+\s+IN\s+/", $TTL . "\tIN ", $line);
            }
        }
        file_put_contents($write_to . "/$filename", implode("\n", $contents));
        echo "$filename updated<br>";
    }
}
?>
2013-05-11 Updated: Couple fixes.
 
Last edited: