#!/usr/bin/perl if ($#ARGV == -1) { print "Usage: $ARGV[-1] real-file\n"; exit 0; } $RealFile = $ARGV[0]; $debug = 1; open (RF,"$RealFile") || die "Can't open $RealFile: $!\n"; open (OF,">$RealFile.new") || die "Can't open $RealFile.new: $!\n"; while () { if (/^\#/) { print OF; next; } if (/^[ \t]*\n$/) { print OF; next; } chomp; ($host,$group,$reallist) = split(':'); @reals = split('\|',$reallist); @hostbreak = split(/\./,$host); $domain = $hostbreak[$#hostbreak-1].".".$hostbreak[$#hostbreak]; if ($debug) { print "\n\nPROCESSING: $host\n"; print "DOMAIN: $domain\n"; print "CURRENT: $host:$group:"; foreach $r (@reals) { print "$r|"; } print "\n"; } # # Reinit the ip hash # delete @newreals{keys %newreals}; delete @oldreals{keys %oldreals}; delete @noreal[0 .. $#noreal]; delete @slbreal[0 .. $#slbreal]; # foreach $ip (keys %newreals) { $newreals{"$ip"} = 0; } # foreach $ip (keys %oldreals) { $oldreals{"$ip"} = 0; } # foreach $i (@noreal) { shift (@noreal); } # foreach $i (@slbreal) { shift (@slbreal); } # # Walk each nameserver for a given host # foreach $nsline (`/usr/bin/host -t ns $domain`) { chomp($nsline); $ns = (split(/[ \t]+/,$nsline))[3]; if ($debug) { print "$domain has a nameserver of $ns\n"; } $redo = 1; $tmphost = $host; while ($redo == 1) { print "Looking up A records for $tmphost\n"; foreach $ipline (`/usr/bin/host -t a $tmphost $ns`) { if ($ipline =~ /nickname/) { chomp ($ipline); @t = split(/[ \t]/,$ipline); $newhost = $t[-1]; undef @t; print "$host is a CNAME to $newhost\n"; $redo = 2; next; } else { if ($redo != 2) { $redo = 0; } } if (!($ipline =~ /has address/)) { next; } chomp($ipline); $ip = (split(/[ \t]+/,$ipline))[3]; print "$tmphost IN A $ip\n"; $newreals{"$ip"} = 1; } if ($redo) { print "Recursing down the CNAME\n"; $tmphost = $newhost; $redo = 1; } } } if ($debug) { foreach $nr (keys %newreals) { print "$host is $nr\n"; } } # # Now see if that differs from what we already have. # If an IP that was in @reals is no longer there, remove it. # If a new IP is found, add it. # # First check for what isn't there anymore... foreach $r (@reals) { if ($newreals{"$r"} == 0) { if ($debug) { print "$r was removed for $host\n"; } push (@noreal,$r); } # Prepare for next foreach $oldreals{"$r"} = 1; } # What's been added? foreach $r (keys %newreals) { if ($oldreals{"$r"} == 0) { if ($debug) { print "$r was added for $host\n"; } push (@slbreal,$r); } } # # Update the new reals file # $cnt = 0; @nrs = keys %newreals; print OF "$host:$group:"; foreach $nr (@nrs) { $cnt++; print OF "$nr"; if ($cnt <= $#nrs) { print OF "|"; } else { print OF "\n"; } } } close (RF); close (OF); rename ("$RealFile", "$RealFile.bak"); rename ("$RealFile.new", "$RealFile");