Telnet SMTP with expect or shell script

Want to build up a Auth Smtp Connection with expect script... just to test I wanted to get ehlo parameters but expect is not working like this

set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect "[2]{2,}[0]{1,}"
send "ehlo\n"

I expect the code 220 to come from mailserver to continue to send ehlo ... just like

..../...:telnet 25
Connected to
Escape character is '^]'.
250-SIZE 41697280

error saying:

spawn telnet 25
invalid command name "2"
    while executing
    invoked from within
"expect "[2]{2,}[0]{1,}""
    (file "./login.exp" line 6)

if I just write expect "220" instead of expect "[2]{2,}[0]{1,}" it works but ignors send "ehlo\n"


You need to send a newline after sending "ehlo":

send "ehlo\n"

EDIT: Based on your latest edit, you also have to escape the leading bracket in your regex to prevent tcp from trying to interpret it as a command:

expect "\[2]{2,}\[0]{1,}"

EDIT: Also, your expect line isn't actually matching what you think it is. At this point, I'd suggest running through on of the many tutorials on expect, or simply use autoexpect to generate your script.

As above adviced I used exp_internal 1 to get sense of what expects really listen to... Also I can recommend autoexpect which created the expect script not perfectly but after improving some codings it is a real help and at last it worked.

#exp_internal 1
set timeout -1
set smtp [lindex $argv 0]
set port [lindex $argv 1]
spawn telnet $smtp $port
expect -re {[2]{2,}[0]{1,}}
sleep 3;
send -- "Ehlo\r"
expect -re {[2]{1,}[5]{1,}[0]{1,}}
send -- "quit\r"
expect eof

Need Your Help

How to export data from the database into .csv format programmatically?

java mysql database csv

I have stored some data in MySQL database through a java program. Now I want to export that data from the same program in .csv file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.