I'm writing a script for work that will dig for DNS records for a
given domain name and put the entries into an array. At the end of the
digging, it outputs the array elements to the screen, asks if
everything looks good, and if so, writes them out to the shell and
builds a zone file. However, I've come across a wierd problem

when I dig for MX records, they may or may not be in my list of
subdomains to dig for, so I call getAforMX(<mx server>) which should
track down the actual A record and push that into my dns_commands
array. However, when it comes back, it fails to do the same for the
remaining MX records

here's the script.
------------<start>----------------
#!/usr/bin/perl

use warnings;
use strict;

my $domain = shift || die "Usage: dns_replicate <domain.name> [name
server [name server [...]]]";
my @servers = @ARGV || qw( ns1.myisp.net ns2.myisp.net );
my @tmp = ();
my @digOut = ();
my @dns_commands = ();
my @subdomains = qw( www citrix server mailserver webserver webmail
mail pop smtp ftp );

# Test to see if we already have a zone file
@tmp = `dns_lookup $domain`;
if ($tmp[0] =~ /MASTER/) {
       print "\n-ERR We appear to already have a zone file for this
domain. exiting\n";
       shift @tmp;
       shift @tmp;
       foreach (@tmp) {
               print;
       }
       print "\n";
       exit;
}

# Now we've got an array of good name servers to try and dig from
# begin building the command array
push @dns_commands, "dns_create $domain noweb\n";

# get the MX records
@digOut = `dig [EMAIL PROTECTED] $domain MX`;
foreach (@digOut) {
       if (/^$domain.+MX\s+(\d+)\s+(.+)/) {
               my $pref;
               if ($1 == "0") {
                       $pref = "1";
               } else {
                       $pref = $1;
               }
               push @dns_commands, "dns_mx_add $domain \@ $pref $2\n";
               getAforMX($2);
       }
}

# Next get the domain's @ record
@digOut = `dig [EMAIL PROTECTED] $domain A`;
foreach (@digOut) {
       if (/^$domain.+\sA\s+(.+)/) {
               push @dns_commands, "dns_a_add $domain \@ $1\n";
       }
}

# Next loop through the subdomains array
foreach my $sd (@subdomains) {
       @digOut = `dig [EMAIL PROTECTED] $sd.$domain`;
       foreach (@digOut) {
               if (/^$sd\.$domain.+\sIN\s+(\w+)\s+(.+)/) {
                       my $rectype = lc($1);
                       push @dns_commands, "dns_".$rectype."_add
$domain $sd $2\n";
               }
       }
}

print "These are the commands I'm about to run. If everything looks good,\n";
print "Press enter. Otherwise, hit ctrl-c to quit.\n\n";

foreach (@dns_commands) {
       print;
}

my $junk = <STDIN>;

foreach (@dns_commands) {
       system("$_");
}

sub getAforMX {
       my $mx = shift || die "Argument mis-match in getAforMX(): No
MX server!";

       if ($mx !~ /$domain\./) {
               # MX record points to another domain, no work to do
       }

       @digOut = `dig [EMAIL PROTECTED] $mx`;
       foreach (@digOut) {
               if (/^(.+)\.$domain\..+\sIN\s+(\w+)\s+(.+)/) {
                       my $rectype = lc($2);
                       push @dns_commands, "dns_".$rectype."_add
$domain $1 $3\n";
               }
       }
}
-----------<end>------------------

Here's some sample output that demonstrates my problem

If I comment out the call to getAforMX(), I get all three mx servers
[EMAIL PROTECTED] ~]$ ./digger microsoft.com
These are the commands I'm about to run. If everything looks good,
Press enter. Otherwise, hit ctrl-c to quit.

dns_create microsoft.com noweb
dns_mx_add microsoft.com @ 10 mailb.microsoft.com. <---gets all three MX records
dns_mx_add microsoft.com @ 10 mailc.microsoft.com.
dns_mx_add microsoft.com @ 10 maila.microsoft.com.
dns_a_add microsoft.com @ 207.46.130.108
dns_a_add microsoft.com @ 207.46.250.119
dns_cname_add microsoft.com www toggle.www.ms.akadns.net.
dns_a_add microsoft.com mail 131.107.1.71
dns_a_add microsoft.com smtp 131.107.115.212
dns_a_add microsoft.com smtp 131.107.115.214
dns_a_add microsoft.com smtp 131.107.115.215
dns_a_add microsoft.com smtp 205.248.106.30
dns_a_add microsoft.com smtp 205.248.106.32
dns_a_add microsoft.com smtp 205.248.106.64
dns_a_add microsoft.com ftp 207.46.236.102


If I do make the call to getAforMX(), it gets the A records for the
first server, then mysteriously exits the foreach loop which surrounds
the call to getAforMX().
[EMAIL PROTECTED] ~]$ ./digger microsoft.com
These are the commands I'm about to run. If everything looks good,
Press enter. Otherwise, hit ctrl-c to quit.

dns_create microsoft.com noweb
dns_mx_add microsoft.com @ 10 maila.microsoft.com.  <-----no other MX
records listed ?
dns_a_add microsoft.com maila 205.248.106.64             <---but it
does get the A records
dns_a_add microsoft.com maila 131.107.115.212
dns_a_add microsoft.com @ 207.46.130.108
dns_a_add microsoft.com @ 207.46.250.119
dns_cname_add microsoft.com www toggle.www.ms.akadns.net.
dns_a_add microsoft.com mail 131.107.1.71
dns_a_add microsoft.com smtp 131.107.115.215
dns_a_add microsoft.com smtp 205.248.106.30
dns_a_add microsoft.com smtp 205.248.106.32
dns_a_add microsoft.com smtp 205.248.106.64
dns_a_add microsoft.com smtp 131.107.115.212
dns_a_add microsoft.com smtp 131.107.115.214
dns_a_add microsoft.com ftp 207.46.236.102

--
I'm nerdy in the extreme and whiter than sour cream

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to