« Cool Bash Trick I Didn't Know About | Main | Awesome Error Message of the Day »

The Trials and Tribulations of Testing SMTP Authentication From Within Bash

Two really random issues trying to write a command line test to ensure a specified username and password are able to authenticate with our SMTP services. First, here is what my final function ended up being:

test_account_smtp(){
USERNAME="$1"
PASSWORD="$2"
USERNAME_BASE64=`echo "use MIME::Base64;
	print encode_base64('$USERNAME');" | perl`
PASS_BASE64=`echo "use MIME::Base64;
	print encode_base64('$PASSWORD');" | perl`
COUNTER=1
while [ "$RETURN" != "0" ] && [ "$COUNTER" != "20" ]
do
	echo "spawn telnet lb.relay 25
		expect \"220 relay*forbids use of this system for unsolicited bulk electronic mail*\"
		send -- \"EHLO mailinstaller\.emailsrvr\.com\r\"
		expect \"*250 8BITMIME*\"
		send -- \"AUTH LOGIN\r\"
		expect \"*334 .*\"
		send -- \"$USERNAME_BASE64\r\"
		expect \"*334 .*\"
		send -- \"$PASS_BASE64\r\"
		expect \"*Authentication successful*\"
		send -- \"QUIT\r\"
		expect \"*Bye*\"
		expect \"*Connection closed by foreign host*\"
		send -- \"\r\"
		expect eof" | expect >> /var/tmp/SMTP_TEST_$1 2>&1
	cat /var/tmp/SMTP_TEST_$1 >> $DEBUG_LOG
	CHECK=`grep "Authentication successful" /var/tmp/SMTP_TEST_$1`
	if [ -z "$CHECK" ]
	then
		echo '!!!'" Could not open SMTP account $1 with password $2; will try again in ten seconds" | tee $DEBUG_LOG
		COUNTER=$(($COUNTER + 1))
		RETURN=1
		sleep 10
	else
		RETURN=0
		continue
	fi
done
rm -f /var/tmp/SMTP_TEST_$1
return $RETURN
}

Yes, it's messy, but it's messy for a reason -- that's what I had to do to get it to work.

You may be asking, what's with those calls to Perl? Well, it seems that OpenSSL has a function to encode a string in base64 (needed to talk to the SMTP server), however it doesn't work. I was originally getting consistently-incorrect auth information and traced it back to OpenSSL returning different output compared to online base64 encoders and Perl's encode_base64() function. What the heck?!

As for the do/while loop, it was originally a for loop, but then the expect script would get stuck in an infinite loop every time. For whatever reason, replacing the for with a while fixed it. Again, I don't get it, but that's what I had to do to get it working.

If anyone out there knows why these two issues might occur, please do enlighten. I'm most curious, but not enough to go trolling around Google for an answer to an issue I already have a workaround for.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 19, 2007 11:39 AM.

The previous post in this blog was Cool Bash Trick I Didn't Know About.

The next post in this blog is Awesome Error Message of the Day.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.34