Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ############################################################ IDENT(1)
- #
- # $Title: Script to test if an e-mail address is valid $
- #
- ############################################################ GLOBALS
- pgm="${0##*/}" # Program basename
- #
- # Global exit status
- #
- SUCCESS=0
- FAILURE=1
- #
- # Command-line options
- #
- TELNET= # -t
- VERBOSE= # -v
- ############################################################ FUNCTIONS
- usage()
- {
- echo "Usage: $pgm [-tv] username@server.com" >&2
- exit $FAILURE
- }
- ############################################################ MAIN
- #
- # Process command-line options
- #
- while getopts tv flag; do
- case "$flag" in
- t) TELNET=1 ;;
- v) VERBOSE=1 ;;
- *) usage # NOTREACHED
- esac
- done
- shift $(( $OPTIND - 1 ))
- [ "$1" != "${1#*@}" ] || usage # NOTREACHED
- #
- # Expect code to talk to the mail server (MX)
- #
- export TELNET
- exec 9<<'EOF_EXPECT'
- ################################################## PARENT GLOBALS
- set email $env(email)
- set domain $env(domain)
- set mx $env(mx)
- set port $env(port)
- set telnet [expr {$env(TELNET) != ""}]
- ################################################## GLOBALS
- set timeout 300
- if {$telnet} {
- set spawn_cmd "telnet"
- } else {
- set spawn_cmd "nc"
- }
- ################################################## MAIN
- fconfigure stdout -buffering none
- spawn {*}$spawn_cmd $mx $port
- expect -re {220} { send "helo hi\n" }
- expect -re {250} { send "MAIL FROM: <root@localhost>\n" }
- expect -re {250} { send "rcpt to: <$email>\n" }
- expect -re {(250|550)} { send "quit\n" }
- expect -re {221 .* closing connection}
- EOF_EXPECT
- EXPECT_CMD=$( cat <&9 )
- export EXPECT_CMD
- #
- # Get the mail server (MX) for the email domain
- #
- export email="$1"
- export domain="${email#*@}"
- mx=
- for m in $( host -t MX "$domain" |
- awk '/mail is handled by/{sub(/\.$/, "", $NF); print $NF}'
- ); do
- port=
- for p in 25 587; do
- [ "$VERBOSE" ] && echo "Testing $m on port $p"
- nc -z $m $p > /dev/null 2>&1 || continue
- port=$p
- break
- done
- if [ ! "$port" ]; then
- [ "$VERBOSE" ] &&
- echo "Unable to connect to $m (ports: 25 587)" >&2
- continue
- fi
- mx=$m
- break
- done
- if [ ! "$mx" ]; then
- echo "Unable to connect to $domain mail server!" >&2
- exit $FAILURE
- fi
- export mx port
- #
- # Contact the mail server and test the recipient address
- #
- [ "$VERBOSE" ] && echo "Connecting to mail server $mx on port $p"
- echo 'eval $env(EXPECT_CMD)' | expect -f- |
- awk '/^(250|550)[ -]/{r=$1}END{exit r!~/^250/}'
- retval=$?
- if [ $retval -eq $SUCCESS ]; then
- echo "$email exists (mx=$mx:$port)"
- else
- echo "$email does not exist (mx=$mx:$port)"
- fi
- exit $retval
- ################################################################################
- # END
- ################################################################################
- #
- # $Copyright: 2015 Devin Teske. All rights reserved. $
- #
- # $Header$
- #
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement