Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

windows - Expect Script Telnet email script

Trying to use Expect Script the Active TCL in Windows to send an email out every it is run. Below is the code I have but there is an error message displayed below and would appreciate any help on how to avoid the problem. Thank you.

#!/usr/bin/expect
# 
exec tclsh "$0" ${1+"$@"}
package require Expect

spawn plink -telnet IP PORT
send "ehlo *******.com
";
send "AUTH LOGIN
";
expect "334 VXNlcm5hbWU6" sleep .1;
send "*************
";
sleep .1;
expect "334 UGFzc3dvcmQ6
"
send "********
";
sleep .1;
expect "235 Authentication succeeded
";
send "MAIL from:******@*******.com
";
expect "250 OK
"
send "RCPT to:********@*********.com
";
expect "250 Accepted
"
send "DATA
";
send "!!!TEXT HERE!!!
";
send ".
";
send "QUIT
";
exit

Getting a error:

Error in startup script

send: spawn id exp2 not open
           while executing
"send "MAIL from:*****@******.com
""
   (file "***.tcl" line 16)

Any ideas on what is wrong here????

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think because the remote host closed the connection and plink exited.

Why don't you use the smtp package of tcllib? It is much easier to use than talking raw smtp over plink with expect.

To summarize: you use plink for telnet, a thing that you can do with tcl sockets already. You talk smtp, something where many libs exists that makes using it much easier.

Example of sending mail with pure tcl:

package require mime
package require smtp

set tok [::mime::initialize 
    -canonical text/plain 
    -header {From [email protected]} 
    -string {Some Text Here}]
::smtp::sendmessage $tok 
    -servers IP 
    -ports PORT 
    -username ****** 
    -password ****** 
    -recipients [email protected] 
    -originator [email protected]
::mime::finalize $tok

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...