Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Expect nc smtp server

Posted by ShipConner (ShipConner), 11 December 2005
Objective: Create a simple SMTP server using Expect and Netcat

Current Issues
1) Characters are echoed twice (Even with stty -echo)
therefor I  am not capturing the expect_out(x,string)

2) The Expect regular expressions (helo, mail from, etc) probably need to be sequenced into seperate expect statements instead of using exp_continue.  

##################
spawn nc -l 25
stty -echo

exp_internal 1


expect -re ".*" {send -- "20 10x.example.com ESMTP\r"}

expect {
       timeout {puts "timed out"; exit 42}
       eof {puts "eof"; exit 99}
       -re "helo*" {send -- "250 10x.example.com\r"; exp_continue}
       -re "mail from*" {send -- "250 ok\r"; exp_continue}
       -re "rcpt to*" {send -- "250 ok\r"; exp_continue}
       -re "dat*" {send -- "354 go ahead\r"; exp_continue}
       -exact "\r\n.\r\n" {send -- "250 ok 1134335146 qp 3108\r"; exp_continue}
       -re "quit" {send -- "221 10x.example.com\r"; exit}
}
#################

If anybody has any ideas or can suggest a better strategy using Netcat and Expect I am open to it.

Posted by ShipConner (ShipConner), 12 December 2005
Sorry to reply to my own topic and SORRY to post bash code in a TCL/Expect forum but I found a more direct solution to my objective(Well I changed that a little too). All the kinks arent' worked out yet but the core functionality is solid.

Bash solution:

1) create the bash script "smtp.sh"
#!/bin/bash

       echo "10x.example.com ESMTP"

       read helo
       echo "250 10x.example.com"

       read mail_from
       echo "250 ok"

       read rcpt_to
       echo "250 ok"

       read data
       echo "354 go ahead"

       read newline_dot_newline
       echo "250 ok 1134335146 qp 3108"

       read quit
       echo "221 10x.example.com"

       echo "$helo" > myfile
       echo "$mail_from" >> myfile
       echo "$rcpt_to" >> myfile
       echo "$data" >> myfile
       echo "$newline_dot_newline" >> myfile
       echo "$quit" >> myfile

2) Setup a netcat listener that will execute the bash script (smtp.sh) perpetualy:
while true
do
     nc -l -p 10101 -e smtp.sh
done

**still need to test variables for expected (hey don't bash me for a pun ...) content

**still need to remove administrative content from varialbes i.e. "mail to: 250 ok, etc when sending to file.

Sorry Graham I  thought I would post anyway to inspire some ideas.  

Posted by admin (Graham Ellis), 12 December 2005
I'm very happy indeed if people post a question and then a solution all while I'm asleep.  And you article will be automatically archived and perhaps found by people searching for something like you're talimg about.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho