User Tools

Site Tools


dns_add_script

This is an old revision of the document!


#!/usr/bin/perl -w
##Jason Thomas
use strict;
use lib '/home/jthomas/lib/perl5/site_perl/5.8.8/';
use File::Copy;
use IO::File;
use Text::Template;
 
my $repository = "/home/jthomas/dns/master/";
my $dnsData = "data";
 
 
 
open (DATA, $dnsData), or die $!;
my @dnsLine = <DATA>;
 
foreach my $entry (@dnsLine){
 
	chomp ($entry);
	my ($hostName, $ipAddress) = split(' ', $entry);
	my ($domainName, $subDomain) = findDomainName($hostName);
	my $dbZoneFile = checkZoneExist($domainName, $subDomain, $ipAddress);
	addToZone($dbZoneFile, $subDomain, $ipAddress);
}
 
close (DATA);
exit (0);
 
sub findDomainName{
 
	my ($hostName) = @_;
	my $domainName;
	my $subDomain;
	my $size;
	my $counter;
	my @tmp;
 
	@tmp = split('\.', $hostName);
	$size = @tmp - 2; 
 
		if ( @tmp == 2){
 
			return ($hostName, undef);
		}
 
		else{
 
			$domainName = "$tmp[@tmp-2].$tmp[@tmp-1]";
 
			for ($counter = 0; $counter < $size; $counter++){
 
				if ( ! defined($subDomain) ){
					$subDomain=$tmp[$counter];
 
				}
				else{
					$subDomain="$subDomain.$tmp[$counter]";
 
				}
			}
			return ($domainName, $subDomain);
		}
}
 
sub checkZoneExist{
 
	my ($domainName, $subDomain, $ipAddress) = @_;
	my $dbZoneFile = "db.$domainName";
	my $userName = "jthomas";
	my $soa = "2009081701";
 
		if ( -e $repository.$dbZoneFile ){
		##	print "File Exists!";	
		}
 
		else{
			my ($tabs) = tabLength($domainName);
			makeZone("customArecord.tmpl", $dbZoneFile,  {soa => \$soa, userName => \$userName, domain => \"$domainName.", tabs => \$tabs, CUSTOMIP => $ipAddress });
		}
 
		return $dbZoneFile;
}
 
 
 
sub makeZone{
 
	my ($templateSource,$dbZoneFile, $fillInHash) = @_;
	my $template = Text::Template->new(SOURCE => $templateSource) or die "Couldn't construct template: $Text::Template::ERROR";
	my $result = $template->fill_in(HASH => $fillInHash);
 
	open(DBFILE, ">$repository/$dbZoneFile") , or die $! ;
	print DBFILE $result; 
	close (DBFILE);
 
	print "\[Info\] Zone file $dbZoneFile created.\n";
 
}
 
sub addToZone{
 
 
	my ($dbZoneFile,  $subDomain, $ipAddress) = @_ ; 
 
 
 
	if ( defined($subDomain) &&  $subDomain ne "www"){
		my ($tabs) = tabLength($subDomain);
		open(DBFILE, ">>$repository/$dbZoneFile") , or die $! ;
		print DBFILE "$subDomain$tabs"."IN\tA\t$ipAddress\n";
		close (DBFILE);
	}	
}
 
sub tabLength{
 
	my ($word) = @_; 
	my $tabs = "\t" x (((32 - length($word) + 6) / 8));
	if (length($tabs) == 0) {
    $tabs = " ";
 
	}
	    return $tabs;
}
dns_add_script.1250543481.txt.gz · Last modified: 2020/08/10 02:28 (external edit)