bashとexpectで簡易Open Relayチェック

TELNET_RESULT=`expect -c " 
set timeout 3
spawn telnet ${TARGET_IP} 25
expect \"\" 
send \"HELO ${HELO_DOMAIN}\n\" 
expect \"${HELO_DOMAIN}\" 
send \"MAIL FROM: ${FROM_MAIL}\n\" 
expect \"Ok\" 
send \"RCPT TO: ${TO_MAIL}\n\" 
expect \"Ok\" 
send \"quit\n\" 
" | tr "\r" "\n"`

if [ `echo "${TELNET_RESULT}" | grep "Relay access denied" 2> /dev/null | wc -l` -eq 0 ]; then
    echo "NG" 
else
    echo "OK"
fi