I am creating a script to test an email by HELO ..
Directly in telnet
console, the commands run fine, but in a script, I can not get the output.
In bash:
$ telnet alt1.aspmx.l.google.com 25
HELO verify-email.org
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
quit
results in:
root@san [~]# telnet alt1.aspmx.l.google.com 25
Trying 2a00:1450:4010:c09::1a...
Connected to alt1.aspmx.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP qm6si6508388lbb.110 - gsmtp
HELO verify-email.org
250 mx.google.com at your service
MAIL FROM: <[email protected]>
250 2.1.0 OK qm6si6508388lbb.110 - gsmtp
RCPT TO: <[email protected]>
550-5.1.1 The email account that you tried to reach does not exist. Please try
550-5.1.1 double-checking the recipient's email address for typos or
550-5.1.1 unnecessary spaces. Learn more at
550 5.1.1 https://support.google.com/mail/answer/6596 qm6si6508388lbb.110 - gsmtp
quit
221 2.0.0 closing connection qm6si6508388lbb.110 - gsmtp
Connection closed by foreign host.
In a script:
cat << EOF | telnet alt1.aspmx.l.google.com 25
HELO verify-email.org
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
quit
EOF
OR
{ echo "HELO verify-email.org"; sleep 1; echo "MAIL FROM: <[email protected]>"; sleep 1; echo "RCPT TO: <[email protected]>" ; sleep 1 ; echo quit } | telnet alt1.aspmx.l.google.com 25
OR
#!/usr/bin/expect -f
spawn alt1.aspmx.l.google.com 25
send '"HELO verify-email.org\r"'
send '"MAIL FROM: <[email protected]>\r"'
send '"RCPT TO: <[email protected]>\r"'
send '"quit\r"'
OR
sh aa 1> aa.txt 2>&1
OR
sh aa &> aa.txt
brings no results.
In general, you probably don't want to pipe things into telnet. A reasonable alternative is netcat
(which is nc
on most systems).
That said, I wrote a tiny bash HTTP client a while back, relying on bash's internal support for making TCP socket connections. An SMTP client is a little more complex, but still fairly easy. SMTP is nice. You can load up a bunch of commands, then just read multiple response lines all at once.
#!/usr/bin/env bash
target="$1"
address="$2"
success=""
# Open a two-way socket connection on fd/3
exec 3<>/dev/tcp/$target/25
# Stand back, we're doing SMTP!
printf "HELO $HOSTNAME\r\n" >&3
printf "MAIL FROM: <$USER@$HOSTNAME>\r\n" >&3
printf "RCPT TO: <$address>\r\n" >&3
printf "QUIT\r\n" >&3
# Now that we've had our say, it's time to listen.
while read -u 3 code message; do
echo ">> <$code> ${message%$'\r'}" # Debugging output
case "$code" in
2??) success="${success:-true}" ;; # Only set variable if it's not already set
5??) success=false ;; # (i.e. false overrides all other responses)
esac
done
# Close connections, clean up after ourselves
exec 3<&-; exec 3>&-
if [ -z "$success" ]; then
echo "NOTICE: Not sure we reached an SMTP server..."
exit 1
elif $success; then
echo "NOTICE: All's well, $target accepts mail for $address"
else
echo "NOTICE: I detected a failure."
exit 1
fi
Note the Parameter Expansion of ${message%$'\r'}
, which strips the last character from the line if it is a CR. This is done because SMTP responses use \r\n
as newlines, whereas your script probably considers the \r
as merely part of the line (or the $message variable).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With