Skip to content

Instantly share code, notes, and snippets.

@omega
Created January 15, 2009 13:35
Show Gist options
  • Save omega/47406 to your computer and use it in GitHub Desktop.
Save omega/47406 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Net::DNS;
my $res = Net::DNS::Resolver->new;
$res->nameservers('10.0.0.10');
my @zone = $res->axfr('intra');
use Data::Dump qw/dump/;
#dump(\@zone);
my %hosts;
my %aliases;
my @resolves;
foreach (@zone) {
if ($_->type eq 'A') {
$hosts{$_->name} = $_->address;
push(@{ $aliases{ $_->name }}, $_->name);
} elsif ($_->type eq 'CNAME') {
unless ( $hosts{ $_->cname }) {
## Need to resolve this!
my $cn = $_->cname;
push(@resolves, $_->cname) unless grep { $_ eq $cn } @resolves;
} else {
# we store this as well
# if ape.intra CNAME dorg.intra CNAME intra A 10.0.0.10 or whatever
$hosts{ $_->name } = $hosts{ $_->cname };
}
push(@{ $aliases{ $_->cname }}, $_->name)
}
}
@resolves = grep { !$hosts{$_} and $_ } @resolves;
# weed it a little, some might have been resolved now
foreach my $d (@resolves) {
my $a = $res->query($d, 'A');
my @a = $a->answer if ($a);
foreach my $r (@a) {
if ($r->type eq 'A') {
$hosts{ $d } = $r->address;
}
}
}
@resolves = grep { !$hosts{$_} } @resolves;
#warn "could not resolve: " . dump(\@resolves) if scalar(@resolves);
foreach my $k (keys %aliases) {
my @list = @{ $aliases{$k} };
# find the ip
my ($ip, $hosts);
$ip = $hosts{$k};
foreach my $n (@list) {
$ip ||= $hosts{$n} if $hosts{$n};
$hosts .= " $n";
$n =~ s/\.intra$//;
$ip ||= $hosts{$n} if $hosts{$n};
$hosts .= " $n";
}
print $ip . "\t" . $hosts . "\n" if $ip;
#warn "UNRESOLVED DOMAINS: $hosts" unless $ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment