On Fri, Oct 11, 2002 at 08:05:49PM -0500, Freddy Chavez wrote:
> I've read many examples about using a shell script to test a mail server
> (sendmail, postfix, etc) such as:
>     #!/bin/bash
>     telnet 1.2.3.4 25 << _EOF_
>     HELO abc.com
>              <- smtp conversation here (mail from, rcpt to, data, etc)
>     _EOF_
> 
> When I execute it, I get:
>     220 xyz.com ESMTP Sendmail 8.11.6/8.11.6; Fri, 11 Oct 2002
> 19:59:20 -0400
>     Connection closed by foreign host.
> 
> I've tried many ways to do it but it doesn't work. I've read somewhere that
> "telnet is not interactive so it will not work". I know Perl is much better
> and gives more control, but I want to do it first en bash script just for
> fun :)  Any suggestion?

The best tool for this type of work is "expect".  It is explicitly designed
for creating scripts that allow one program to interact with another.

The problem with your script above is that it send all of your text to
the smtp host immediately without waiting for the SMTP program to
get ready for it.  As a result the SMTP program drops all your text on
the floor.

Here is an example of a program which gets the count of messages for a user on
a pop host.

#!/usr/bin/expect 

#  turn off trace output
log_user 0

# launch the telnet program, specifying host and port number
eval spawn telnet -l myname pop.mydomain.com 110

# Wait until the pop host sends back the ready string
expect "ready."

#  Send the login sequence to the pop host
send "user myname\r"


#  Wait for the pop host prompt for a password
expect "PASS"

send "pass mypassword\r"

expect "welcome"

send "stat\r"

expect "+OK "


# Yes -expect can use wildcards and can even use a "case" statement to respond
# to variable output in variable ways.
expect -re "\[0-9]* "

#  Save data for some future use.
set answer $expect_out(0,string)

#  Send_user sends the text to the user terminal, instead of the interacting
#  program.
send_user "$answer \r\n"

expect -re "\[0-9]*\r\n"

send "quit\r"

expect "signing off"
exit
#
#  



-- 
Jeff Kinz, Director, Emergent Research,  Hudson, MA.  "[EMAIL PROTECTED]" 
"[EMAIL PROTECTED]" copyright 2002.  Use is restricted. Any use is an 
acceptance of the offer at http://users.rcn.com/jkinz/policy.html.

    (o-                                    -o)
    //\         eLviintuaxbilse            /\\
    V_/_                                  _\_V   



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to