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
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho