Today is: 8 January, 2012
Check todays hot topics

Generate DNS A/PTR Records from DHCPd config

#!/usr/bin/perl
#
# Genreate A/PTR records off dhcpd config
# 
# Assumes the following layout
#
#  host host1-vlan1 {
#       hardware ethernet 00:16:3e:10:11:b3;
#       fixed-address 10.74.17.29;
#  }
# 
#  host host2-vlan2 {
#        hardware ethernet 00:16:3e:12:ee:79;
#        fixed-address 10.74.17.30;
#  }
 
use strict;
use warnings;
 
use constant DOMAIN => 'domain.com';
use constant LINES_BETWEEN_MATCHES => 2;
 
my $file = '/etc/dhcpd.conf';
 
if (( ! -e $file ) || ( ! -r $file ))
{
	print STDERR "Unable to read/open dhcpd config.\n";
	exit 1;
}
 
open(FILE, $file);
my @lines = <FILE>;
close(FILE);
 
my %hash;
my $x = 0;
foreach my $line (@lines)
{
	$x++;
	if ($line =~ m/host\ (.*)\ {/)
	{
		my @split = split('-', $1);
		push(@{$hash{$x}}, join('.', @split));
	}
	push(@{$hash{$x-&LINES_BETWEEN_MATCHES}}, $1) if ($line =~ m/fixed-address\ (.*);/);
}
 
foreach my $zone (qw(FORWARD REVERSE))
{
	print "$zone:\n\n";
	foreach my $key (sort { $a <=> $b } keys %hash)
	{
		print "$hash{$key}[0]\t\tA\t$hash{$key}[1]\n" if ($zone eq 'FORWARD');
		if ($zone eq 'REVERSE')
		{
			my @octets = split(/\./, $hash{$key}[1]);
			print "$octets[3]\t\tPTR\t$hash{$key}[0]." . DOMAIN . ".\n";
		}
	}
	print "\n";
}
 
exit 0;