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).
ghoti source share