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
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
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 ESMTP\r"}

expect {
       timeout {puts "timed out"; exit 42}
       eof {puts "eof"; exit 99}
       -re "helo*" {send -- "250\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\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 ""

       echo " ESMTP"

       read helo
       echo "250"

       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"

       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 ( perpetualy:
while true
     nc -l -p 10101 -e

**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 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: • WEB: • SKYPE: wellho