Save SMTP session from telnet in log with shell script

I am creating a script to check HELO email ..

Directly in the telnet console, the commands work fine, but in the script I can not get the output.

In bash:

 $ telnet alt1.aspmx.l.google.com 25 HELO verify-email.org MAIL FROM: < check@verify-email.org > RCPT TO: < test@gmail.com > quit 

leads to:

 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: < check@verify-email.org > 250 2.1.0 OK qm6si6508388lbb.110 - gsmtp RCPT TO: < test@gmail.com > 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 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 the script:

 cat << EOF | telnet alt1.aspmx.l.google.com 25 HELO verify-email.org MAIL FROM: < check@verify-email.org > RCPT TO: < test@gmail.com > quit EOF 

OR

 { echo "HELO verify-email.org"; sleep 1; echo "MAIL FROM: < check@verify-email.org >"; sleep 1; echo "RCPT TO: < test@gmail.com >" ; 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: < check@verify-email.org >\r"' send '"RCPT TO: < test@gmail.com >\r"' send '"quit\r"' 

OR

 sh aa 1> aa.txt 2>&1 

OR

 sh aa &> aa.txt 

no results.

+5
source share
1 answer

In general, you probably do not want to transfer data to telnet. A reasonable alternative is netcat (which is nc for most systems).

However, I wrote a tiny bash HTTP client some time ago, relying on internal bash support for creating TCP socket connections. The SMTP client is a bit more complicated, but still pretty easy. SMTP is good. You can load a bunch of commands, and then just read several lines of response 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 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 not already set 5??) success=false ;; # (ie 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 well, $target accepts mail for $address" else echo "NOTICE: I detected a failure." exit 1 fi 

Pay attention to the extension of the parameter ${message%$'\r'} , which removes the last character from the string, if it is CR. This is because SMTP responses use \r\n as newlines, while your script probably considers \r just part of the line (or the $ message variable).

+3
source

Source: https://habr.com/ru/post/1244974/


All Articles