You are here

recursive dns resolver script in tcl

This script deals with the fact that cnames are sometimes the answers to dns queries.
For example, in the below dig we get dns-any-yadayada. We don't want that, we want the IP address. The script recursively digs until it gets the ip address.

$ dig +short @8.8.8.8 www.yahoo.com

fd-fp3.wg1.b.yahoo.com.
ds-fp3.wg1.b.yahoo.com.
ds-any-fp3-lfb.wa1.b.yahoo.com.
ds-any-fp3-real.wa1.b.yahoo.com.
206.190.36.45
206.190.36.105

#!/usr/bin/tclsh
proc resolve_this { fqdn server {answer ""} } {
#puts "resolving $fqdn at $server"
set l1 [exec dig +short @$server $fqdn a]
#puts l1:$l1
foreach i $l1 {
set i [string tolower $i]
set x [regexp {[a-z]} $i]
#puts " i:$i x:$x"
if { $x } {
#puts " asking $i"
resolve_this $i $server $answer
} else {
set answer "$answer\n$i"
#puts " answer:$i"
}
}
return $answer
}

set final_answer [resolve_this [lindex $argv 0] [lindex $argv 1]]
puts [string trim $final_answer]