User Tools

Site Tools


dns_add_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision Both sides next revision
dns_add_script [2009/08/14 20:45]
jt created
dns_add_script [2009/08/17 21:11]
jt
Line 1: Line 1:
 <code perl> <code perl>
- 
 #​!/​usr/​bin/​perl -w #​!/​usr/​bin/​perl -w
 ##Jason Thomas ##Jason Thomas
 use strict; use strict;
 +use lib '/​home/​jthomas/​lib/​perl5/​site_perl/​5.8.8/';​
 use File::Copy; use File::Copy;
 use IO::File; use IO::File;
-##use Text::​Template;​+use Text::​Template;​
  
-my $entry; +my $repository = "/​home/​jthomas/​dns/​master/";
-my $hostName;​ +
-my $ipAddress;​ +
-my $domainName;​ +
-my $subDomain;​ +
-my $dbZoneFile;​ +
-my $repository = "/​home/​jthomas/​dns/​master";​+
 my $dnsData = "​data";​ my $dnsData = "​data";​
 +
 +
 +
 open (DATA, $dnsData), or die $!; open (DATA, $dnsData), or die $!;
 my @dnsLine = <​DATA>;​ my @dnsLine = <​DATA>;​
  
-foreach $entry (@dnsLine){+foreach ​my $entry (@dnsLine){
  
  chomp ($entry);  chomp ($entry);
- ($hostName,​ $ipAddress) = split('​ ', $entry); + my ($hostName, $ipAddress) = split('​ ', $entry); 
- $subDomain = findDomainName($hostName);​ + my ($domainName, ​$subDomain= findDomainName($hostName);​ 
-## print "$domainName\n";​ + my $dbZoneFile = checkZoneExist($domainName$subDomain$ipAddress);
- checkZoneExist($domainName); +
- +
- if ( defined($subDomain) &&  ​$subDomain ne "​www"​){+
  addToZone($dbZoneFile,​ $subDomain, $ipAddress);​  addToZone($dbZoneFile,​ $subDomain, $ipAddress);​
- } 
 } }
  
 close (DATA); close (DATA);
- 
 exit (0); exit (0);
  
 sub findDomainName{ sub findDomainName{
  
- my $hostName = shift;+ my ($hostName@_; 
 + my $domainName;
  my $subDomain;  my $subDomain;
  my $size;  my $size;
Line 46: Line 39:
  @tmp = split('​\.',​ $hostName);  @tmp = split('​\.',​ $hostName);
  $size = @tmp - 2;   $size = @tmp - 2; 
-## print "size of array: $size.\n";​ 
  
- if ( @tmp==2){ + if ( @tmp == 2){ 
- $domainName = "​$tmp[0].$tmp[1]";​ + 
- return undef+ return ​($hostName, ​undef);
-## print "​$domainName\n"​;+
  }  }
 +
  else{  else{
 +
  $domainName = "​$tmp[@tmp-2].$tmp[@tmp-1]";​  $domainName = "​$tmp[@tmp-2].$tmp[@tmp-1]";​
  
Line 60: Line 53:
  if ( ! defined($subDomain) ){  if ( ! defined($subDomain) ){
  $subDomain=$tmp[$counter];​  $subDomain=$tmp[$counter];​
 +
  }  }
  else{  else{
  $subDomain="​$subDomain.$tmp[$counter]";​  $subDomain="​$subDomain.$tmp[$counter]";​
 +
  }  }
  }  }
-## print "​$subDomain.$domainName\n"; + return ($domainName$subDomain);
- return ​$subDomain;+
  }  }
 } }
Line 72: Line 66:
 sub checkZoneExist{ sub checkZoneExist{
  
- my $domainName = shift;+ my ($domainName, $subDomain, $ipAddress) ​@_;
  my $dbZoneFile = "​db.$domainName";​  my $dbZoneFile = "​db.$domainName";​
 + my $userName = "​jthomas";​
 + my $soa = "​2009081701";​
  
  if ( -e $repository.$dbZoneFile ){  if ( -e $repository.$dbZoneFile ){
- ## print "$dbZoneFile exists\n";+ ## print "File Exists!";
  }  }
 +
  else{  else{
- ##​ print ​"​$dbZoneFile ​does not exists\n"; + my ($tabs) = tabLength($domainName);​ 
 + makeZone("customArecord.tmpl", ​$dbZoneFile,  {soa => \$soa, userName => \$userName, domain => \"$domainName.",​ tabs => \$tabs, CUSTOMIP => $ipAddress });
  }  }
 +
 + return $dbZoneFile;​
 +}
  
- return $dbZoneFile;​ 
  
- 
-} 
  
 sub makeZone{ 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";​
  
 } }
Line 96: Line 101:
 sub addToZone{ sub addToZone{
   
- $dbZoneFile = shift; ​ 
- $subDomain = shift; 
- $ipAddress = shift; 
   
- print "$subDomain\t\tIN\tA\t$ipAddress\n";+ 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; 
 +}
 </​code>​ </​code>​
dns_add_script.txt · Last modified: 2020/08/10 02:35 (external edit)