#! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh 'mx' <<'END_OF_FILE' X#!/usr/local/tcl/expect -- X X########################################### X# mx -- Gene Spafford 1/3/95 X# X# Usage: mx [-a] [-i] [-t #] hostname [hostname ...] X# This uses the nslookup program and the current DNS server to X# find the authoritative DNS server for the name, then returns X# any MX records for that name. X# X# Multiple hostnames per line are supported. X# X# The -i flag returns the IP number of the MX servers, too. X# The -t # allows the user to specify timeout values X# The -a flag requests an authoritative answer (usually overkill) X# X########################################### X X# First, define an error routine X Xproc bomb {} { X global argv0 X send_error "usage: [file tail $argv0] \[-a] \[-i] \[-t #] \[...]\n" X exit 1 X} X X# set some default parameters before parsing arguments Xset find_ip 0 Xset authority 0 Xset timeout 20 X Xsource "/homes/spaf/lib/tcl/getopts" Xif {[getopts "ait:" argv options] > 0} bomb X Xif {[info exists options(t)]} then {set timeout $options(t)} Xif {[info exists options(i)]} then {set find_ip 1} Xif {[info exists options(a)]} then {set authority 1} X X X# be sure there are some hosts listed! X Xif {[string length $argv] < 1} bomb X X# now, make a list of the names to query X Xset queries [list $argv] Xset argc [llength $queries] X X# set some other parameters and start up the child process X Xlog_user 0 Xset servers "" X Xspawn -noecho nslookup Xexpect_after { X default { X send_error "Timeout/premature e-o-f!\n" X exit 1 X } X -re "\\\*\\\*\\\*.*\n" { X send_error "$expect_out(buffer)\n" X continue X } X} X X X# Now, gobble up the information to the first prompt X Xexpect { X -re "Default Server:\[ \t]+(.*)\r" { X set def_server $expect_out(1,string) X exp_continue X } X "*> " X} X Xsend "set recurse\rset q=MX\r" Xexpect "> " X Xforeach name $queries { X catch {unset servers} X X if {$authority} { X send "$name\r" X expect "*\n\r\n" X expect "Non*answer:" X X expect "*Authoritative*from:"; X expect { X -re "\n(\[^ \t>]+)" { X lappend servers $expect_out(1,string) X exp_continue X } X "\n> " ;# end condition X } X } X X lappend servers $def_server X X foreach server $servers { X send "$name $server\r" X expect "*\n\r\n" X expect { X timeout continue X -re "^\[^\n]+tative answer\[^\n]+\n" { X exp_continue X } X -re "^\[^\n]+preference =\[^\n]+\n" { X send_user $expect_out(buffer) X exp_continue X } X -re "^\[^\n]+\tinet address =\[^\n]+\n" { X if {$find_ip} {send_user $expect_out(buffer)} X exp_continue X } X "> $" break X } X } X X if {[incr argc -1] > 0} send_user "\n" X} X Xexit 0 X END_OF_FILE if test 2608 -ne `wc -c <'mx'`; then echo shar: \"'mx'\" unpacked with wrong size! fi chmod +x 'mx' # end of 'mx' fi if test -f 'getopts' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'getopts'\" else echo shar: Extracting \"'getopts'\" \(1533 characters\) sed "s/^X//" >'getopts' <<'END_OF_FILE' X# getopts -- like the library routine X# sets flags based on the options available X X Xproc getopts {opts Argv Options} { X upvar $Argv argv $Options options X catch {unset options} X X set errflag 0 X X while {[string match "-*" $argv]} { X regexp "^-(\[^ ]+)( +(.*)?)?$" $argv junk block junk newargv X set argv $newargv X if {[string match "-" $block]} break X X for {set index 0} {$index < [string length $block]} {incr index} { X set char [string range $block $index $index] X set junk [string first $char $opts] X if {$junk < 0} { X puts stderr "getopt: unknown option '$char'\n" X incr errflag X } else { X set options($char) 1 X incr junk X if {! [string match ":" [string range $opts $junk $junk]]} { X continue X } X# X# here is the case where we expect a mandatory argument to the option X# X# if there is anything left in the block, that is the argument X# otherwise, we take the next argument in the line X# otherwise, it is an error X X if {[incr index] < [string length $block]} { X set options($char) [string range $block $index end] X } else { X set pos [string first " " $argv] X if {$pos < 0} { X if {[string length $argv] < 1} { X puts stderr "getopt: missing argument for '$char' option\n" X incr errflag X } else { X set options($char) $argv X set argv "" X } X } else { X set options($char) [string range $argv 0 [expr pos - 1]] X set argv [string range $argv [incr pos] end] X } X } X break X } X } X } X return $errflag X} END_OF_FILE if test 1533 -ne `wc -c <'getopts'`; then echo shar: \"'getopts'\" unpacked with wrong size! fi # end of 'getopts' fi echo shar: End of shell archive. exit 0