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 @

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]