1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#!/usr/bin/perl
# This is a Perl script which uses askmara to recursively look
# for a host name. This is written because I am not constantly
# on line. Hence, this allows me to quickly get a lookup trace for
# a given host name
# This script is not 100% perfect, and is more designed to be a tool
# which I personally use to make offline copies of RRs which cause
# problems with MaraDNS than to be a bugfree tool for recursively looking
# up hostnames. Hence, this has bugs such as the inability to handle a
# dead host, etc. Bug reports which discuss bugs in this tool will be
# cheerfully ignored.
$query = shift || "Awww.google.com.";
$rootns = shift || "198.41.0.4";
$depth = shift || 1;
if($depth > 16) {printf "Recursion limit exceeded\n";exit}
$ns = $rootns;
$visited = {};
for($count=0;$count<128;$count++) {
sleep(1); # Bug with the NAT code...
$na = 0; $place = 0; $nsname = ""; $cname = "";
if($visited{$ns}) {print "# Already visited $ns, exiting\n";exit}
$query =~ s/[^A-Za-z0-9_\-\.]//g;
$ns =~ s/[^A-Za-z0-9_\-\.]//g;
open(ASKMARA,"askmara $query $ns |") || die;
$visited{$ns} = 1;
while(<ASKMARA>) {
print;
chop;
if(/Hard Error/) {exit;}
if(/^\#?C([^|]*)/ && $place == 0 && $query !~ /^C/) {
$place = 6;
}
if($place == 6 && $query =~ /^A/ && /^\#?C[^|]*\|\d+\|(.*)/) {
$place = 0;
$cname = $1;
}
if(/NS replies/) {$place = 1}
if(/AR replies/ && $place != 2) {$place = 3}
if(/^\#S/ && $place == 1) {$place = 2}
if(/^Record name:/ && $place == 5) {$place = 1}
if(/^\#?N/ && $place == 1 && !$nsname) {$place = 5}
if(/^\#?N[^|]+\|\d+\|(.*)/ && $place == 5) {$nsname = $1;$ns=""}
if(/^Record name:/ && $place == 4) {$place = 3}
if(/^\#?A$nsname/ && $place == 3) {$place = 4}
if(/^\#?[^|]+\|\d+\|(.*)/ && $place == 4) {$ns = $1}
}
close(ASKMARA);
# Gluelessness
if(!$ns) {
$depth++;
$nsname =~ s/[^A-Za-z0-9_\-\.]//g;
$rootns =~ s/[^A-Za-z0-9_\-\.]//g;
$depth =~ s/[^A-Za-z0-9_\-\.]//g;
$command = "$0 A$nsname $rootns $depth";
print "\n# Out-of-bailiwick NS record\n";
print "# Glueless level now at $depth\n";
open(COMMAND,"$command |") || die;
$cplace = 1;
$cna = 0;
while(<COMMAND>) {
print;
chop;
if(/^Num Answers: (\d+)/) { $cna = $1 }
if(/^NS replies/) { $cplace = 2 }
if(/^A[^|]+\|\d+\|(.*)/ && !$ns && $cplace == 1) { $ns = $1 }
}
close(COMMAND);
print "\n# Exiting glueless level $depth\n";
$depth--;
}
# Cnames also make things go glueless
if($cname) {
$depth++;
$nsname =~ s/[^A-Za-z0-9_\-\.]//g;
$rootns =~ s/[^A-Za-z0-9_\-\.]//g;
$depth =~ s/[^A-Za-z0-9_\-\.]//g;
$command = "$0 A$cname $rootns $depth";
print "\n# CNAME record\n# Glueless level now at $depth\n";
open(COMMAND,"$command |") || die;
$cplace = 1;
$cna = 0;
while(<COMMAND>) {
print;
chop;
if(/^Num Answers: (\d+)/) { $cna = $1 }
if(/^AN replies/) { $cplace = 1 }
if(/^NS replies/) { $cplace = 2 }
if(/^A[^|]+\|\d+\|(.*)/ && !$ns && $cplace == 1) { $ns = $1 }
}
close(COMMAND);
print "\n# Exiting glueless level $depth\n";
exit;
}
if($na > 0 || $place == 2) {exit;}
}
|