We're in the process of migrating from NIS+ to LDAP where I work, and have decided to forego using LDAP to store host information and deployed a DNS server. Even with all of NIS+' quirkiness and idiosyncratic behavior, it was still a fairly simple naming service to administer. As a result of deploying a DNS server for host information, we lost our quick access to the hosts table for forward and reverse lookups. Rather than having to perform host searches on the BIND zone configuration file, I wrote a quick Perl script to query and parse the name server zone transfer output:
#!/usr/bin/perl
# Simple hash of a few different OSs
%dig = (
solaris => "/usr/sbin/dig",
linux => "/usr/bin/dig",
darwin => "/usr/bin/dig"
);
$DIG = $dig{"$^O"};
$DOMAIN = "example.com";
$DNS = "ns1.example.com";
# Zone transfers are required
$DIGCMD = qq/$DIG \@$DNS $DOMAIN axfr/;
open DIG, "$DIGCMD|" or die "$DIG: $!\n";
while (<DIG>) {
next if (/^;/); # Skip any comments
# If we match a CNAME record, we have an alias to something.
# $1 = alias (CNAME), $2 = canonical hostname
if (/^(\S+)\.$DOMAIN\.\s+\d+\s+IN\s*CNAME\s+(\S+)\.$DOMAIN\.$/) {
# Push an alias (CNAME) onto an array indexed on canonical hostname
push(@{$cnames{$2}}, $1);
}
# Here's a standard A (canonical hostname) record
# $1 = canonical hostname, $2 = IPv4 address
if (/^(\S+)\.$DOMAIN\.\s+\d+\s+IN\s*A\s+(\S+)$/) {
$ip{$1} = $2;
}
}
close DIG;
# Format and display it like niscat hosts:
# canonicalHostname alias1 [alias2 aliasN] ipAddress
for $host (sort keys %ip) {
print "$host ";
if (defined(@{$cnames{$host}})) {
print join(' ', @{$cnames{$host}});
print " ";
}
print "$ip{$host}\n";
}
exit 0;
Note that this does require that the nameserver allow zone transfers.