Advertisement
devinteske

email_test.sh

Nov 18th, 2015
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.97 KB | None | 0 0
  1. #!/bin/sh
  2. ############################################################ IDENT(1)
  3. #
  4. # $Title: Script to test if an e-mail address is valid $
  5. #
  6. ############################################################ GLOBALS
  7.  
  8. pgm="${0##*/}" # Program basename
  9.  
  10. #
  11. # Global exit status
  12. #
  13. SUCCESS=0
  14. FAILURE=1
  15.  
  16. #
  17. # Command-line options
  18. #
  19. TELNET=     # -t
  20. VERBOSE=    # -v
  21.  
  22. ############################################################ FUNCTIONS
  23.  
  24. usage()
  25. {
  26.     echo "Usage: $pgm [-tv] username@server.com" >&2
  27.     exit $FAILURE
  28. }
  29.  
  30. ############################################################ MAIN
  31.  
  32. #
  33. # Process command-line options
  34. #
  35. while getopts tv flag; do
  36.     case "$flag" in
  37.     t) TELNET=1 ;;
  38.     v) VERBOSE=1 ;;
  39.     *) usage # NOTREACHED
  40.     esac
  41. done
  42. shift $(( $OPTIND - 1 ))
  43.  
  44. [ "$1" != "${1#*@}" ] || usage # NOTREACHED
  45.  
  46. #
  47. # Expect code to talk to the mail server (MX)
  48. #
  49. export TELNET
  50. exec 9<<'EOF_EXPECT'
  51.  
  52.     ################################################## PARENT GLOBALS
  53.  
  54.     set email   $env(email)
  55.     set domain  $env(domain)
  56.     set mx      $env(mx)
  57.     set port    $env(port)
  58.     set telnet  [expr {$env(TELNET) != ""}]
  59.  
  60.     ################################################## GLOBALS
  61.  
  62.     set timeout 300
  63.  
  64.     if {$telnet} {
  65.         set spawn_cmd   "telnet"
  66.     } else {
  67.         set spawn_cmd   "nc"
  68.     }
  69.  
  70.     ################################################## MAIN
  71.  
  72.     fconfigure stdout -buffering none
  73.     spawn {*}$spawn_cmd $mx $port
  74.  
  75.     expect -re {220} { send "helo hi\n" }
  76.     expect -re {250} { send "MAIL FROM: <root@localhost>\n" }
  77.     expect -re {250} { send "rcpt to: <$email>\n" }
  78.     expect -re {(250|550)} { send "quit\n" }
  79.     expect -re {221 .* closing connection}
  80.  
  81. EOF_EXPECT
  82. EXPECT_CMD=$( cat <&9 )
  83. export EXPECT_CMD
  84.  
  85. #
  86. # Get the mail server (MX) for the email domain
  87. #
  88. export email="$1"
  89. export domain="${email#*@}"
  90. mx=
  91. for m in $( host -t MX "$domain" |
  92.     awk '/mail is handled by/{sub(/\.$/, "", $NF); print $NF}'
  93. ); do
  94.     port=
  95.     for p in 25 587; do
  96.         [ "$VERBOSE" ] && echo "Testing $m on port $p"
  97.         nc -z $m $p > /dev/null 2>&1 || continue
  98.         port=$p
  99.         break
  100.     done
  101.     if [ ! "$port" ]; then
  102.         [ "$VERBOSE" ] &&
  103.             echo "Unable to connect to $m (ports: 25 587)" >&2
  104.         continue
  105.     fi
  106.     mx=$m
  107.     break
  108. done
  109. if [ ! "$mx" ]; then
  110.     echo "Unable to connect to $domain mail server!" >&2
  111.     exit $FAILURE
  112. fi
  113. export mx port
  114.  
  115. #
  116. # Contact the mail server and test the recipient address
  117. #
  118. [ "$VERBOSE" ] && echo "Connecting to mail server $mx on port $p"
  119. echo 'eval $env(EXPECT_CMD)' | expect -f- |
  120.     awk '/^(250|550)[ -]/{r=$1}END{exit r!~/^250/}'
  121. retval=$?
  122. if [ $retval -eq $SUCCESS ]; then
  123.     echo "$email exists (mx=$mx:$port)"
  124. else
  125.     echo "$email does not exist (mx=$mx:$port)"
  126. fi
  127. exit $retval
  128.  
  129. ################################################################################
  130. # END
  131. ################################################################################
  132. #
  133. # $Copyright: 2015 Devin Teske. All rights reserved. $
  134. #
  135. # $Header$
  136. #
  137. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement