Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Mass Domain Name Lookup

by CharlesClarkson (Curate)
on Nov 16, 2001 at 20:54 UTC ( #125849=note: print w/ replies, xml ) Need Help??

in reply to Mass Domain Name Lookup

You're opening and closing DATAOUT too often. How about loading an array and doing the write to file just once.

sub lookup { my ($ldns, $fqdn) = @_; my ($error, $error_a); open DIG, "$dig \@$ldns $type $fqdn | " or die $!; while (<DIG>) { if (/;; ->>HEADER<<- opcode: QUERY\s|QUERY, status: NOERRO +R, .*/) { $error = 1; next; } next unless /$fqdn\.\s+(\w+)(\s+A|\s+IN\sA)\s+(.*)/; $error_a = 1; my $time = lc($1); my $ip = $3; my $seconds = $time =~ /[dhms]/ ? string_to_time($time) : +$time; my $extended_time = time_to_string($seconds); print "Request: $fqdn\tTTL: $extended_time\tIP: $ip\n" if +$verbose; push @data_out, "$ldns\t$fqdn\t$seconds\t$ip\n" if $outfil +e; } close DIG; if (!$error) { print "Request: $fqdn\tERROR (No domain found)\n" if $verbose; push @data_out, "$ldns\t$fqdn\tERROR\tNO DOMAIN\n" if $outfile +; } elsif (!$error_a) { print "Request: $fqdn\tERROR (Domain found, no A record found) +\n" if $verbose; push @data_out, "$ldns\t$fqdn\tERROR\tNOT A RECORD\n" if $outf +ile; } }

As you can see, the code is tighter and easier to read. But, we now must add a test for @data_out.

if (@data_out) { open (DATAOUT, ">> $outfile") || die "can't open $outfile: $!"; print DATAOUT @data_out; close DATAOUT; }

This can be rewritten by taken advantage of the x operator.

foreach $thing (@ns){ push (@provide, "Unknown"); }
push @provide, ('Unknown') x @ns;

And, finally, all those ifs in the beginning could be combined.

if ( $help || !@ns && !$nsfile || !@addr && !$addrfile || !$verbose && !$outfile ) { print <<END; . . .

Charles K. Clarkson

Comment on Re: Mass Domain Name Lookup
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Mass Domain Name Lookup
by argus (Acolyte) on Nov 16, 2001 at 23:10 UTC
    Outstanding! Thank you. I gave you credit in the copy of the code I have here. This helped me learn a lot.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://125849]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2015-11-25 13:03 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (675 votes), past polls