#!/opt/sfw/bin/expect -- #Perform a DNS zone transfer. if {$argc != 1} { puts stderr "$argv0: requires one command line argument\n" exit 1 } set domain [lindex $argv 0] send_user "Connecting to $domain...\n" log_user 0 set timeout 10 ;#default 10 seconds; -1 wait forever; 0 no wait spawn /usr/sbin/nslookup expect "> $" send "set type=any\r" expect "> $" send "$domain\r" set nameservers {} while {1} { expect { "> $" { break } -re "$domain\[ \]+nameserver = (\[^\r\n\]+)\r\n" { lappend nameservers $expect_out(1,string) } } } #Remove duplicates from the list of nameservers. for {set i 0} {$i < [llength $nameservers] - 1} {incr i} { for {set j [expr $i + 1]} {$j < [llength $nameservers]} {incr j} { if {[lindex $nameservers $i] == [lindex $nameservers $j]} { #Remove element number j from the list. set nameservers [lreplace $nameservers $j $j] } } } send_user "Found the following [llength $nameservers] nameservers:\n" foreach nameserver $nameservers { send_user "\t$nameserver\n" } send_user "\n" log_user 1 foreach nameserver $nameservers { send "server $nameserver\r" expect "> $" send "ls $domain\r" expect -re "\\*\\*\\* Can't list domain $domain: Unspecified error\r\n" { expect "> $" continue } break ;#want only one zone transfer } send "exit\r" exit 0