Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Created September 23, 2011 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandhoffman/1238543 to your computer and use it in GitHub Desktop.
Save evandhoffman/1238543 to your computer and use it in GitHub Desktop.
Converts a spreadsheet of hostname->IP/hostname->hostnames to multiple zone files.
#!/usr/bin/perl
my %domains = ();
while(<>) {
chomp;
my @a = split(/\t/);
my ($host, $dom, $points_to,$unused,$crap) = map { s/^([\W\s]+)(.+)/$2/g ; $_; } @a;
#next if $unused;
# Extract the host/domain
my $h, $d;
if ($host =~ /((.+)\.)?(([\w\-]+)\.([\w\-]+))$/) {
$h = $2 ? $2 : '@';
$d = $3;
# print "Host: $h\tDom: $d\n";
}
#print "$line[0]\t$line[1]\t$line[2]\n";
# Assume all records that point to IPs are A records
# All others are CNAMEs
if ($points_to =~ /((\d{1,3})(\.)?){4}/g) {
push( @{$domains{$d}}, ($unused ? ';' : '') . "$h\t\tA\t$points_to");
} else {
push( @{ $domains{$d}}, ($unused ? ';' : '') . "$h\t\tCNAME\t$points_to.");
}
}
foreach $dom (keys %domains) {
# print "Domain: $dom\n";
print <<__END__;
\$ORIGIN $dom.
\$ttl 1h
@ IN SOA ns1.$dom. sysadmins.example.com. (
2011072802 ; Serial
3h ; Refresh after 3 hours
1h ; Retry after 1 hour
4w ; Expire after 4 weeks
1d ) ; Minimum TTL of 1 day
__END__
foreach $record (@{$domains{$dom}}) {
print "$record\n";
}
print "----------- CUT HERE -------------\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment