Re: Trying to write a password auto-entry expect script



O. And my expect is version 5.38.0 running on Mac OS X 10.3.9.



Hi,

I am just starting out with expect and running into some difficulties. The man page, FAQ, and Googling haven't helped me.

I am trying to write a script to invoke mysql, automatically provide the password, and then give control to the user, terminating with the exit status of mysql.

I started with this simple script:

#!/usr/bin/expect -f
spawn -noecho mysql -u user -p db
set timeout 20
expect "Enter password:" { send "pass\r" ; interact } \
timeout { exit 1 } \
eof { exit 1 }
exit [lindex [wait] 3]

which works fine when I run it as ./script. It enters the password, and hands control over to me. I can interact with mysql and when I finish I type Ctrl-D or quit and the program exits. I can test it with a tiny C program I wrote to ensure the exit status is returned correctly, too.

However, when I run it as ./script < input > output it just hangs. It seems that interact does not pass the eof through to mysql, but just returns. As mysql does not receive the eof, it doesn't exit, but just waits for more input; and since expect waits for mysql to exit, nothing happens for a long time...

I seem to be able to force an eof to be sent to mysql like this

#!/usr/bin/expect -f
spawn -noecho mysql -u user -p db
set timeout 20
expect "Enter password:" {
send "pass\r" ; interact eof { send "\u0004" ; return }
} \
timeout { exit 1 } \
eof { exit 1 }
expect eof
exit [lindex [wait] 3]

(though for some reason it seems to want the 'expect eof' before the wait and exit; without it it hangs; I guess perhaps mysql is blocking with a full output buffer and will not exit until the output is consumed).

Now I have two problems with this:

1. I would like eventually to make the channel transparent, and I don't think sending \u0004 will work in that case. Is there a better way to send EOF to a process (i.e. close its stdin)? (Note the following problem, though, also.)

2. I need to capture the output of mysql and send it out stdout. Expect is clearly receiving the output as can be seen with the expect -d diagnostics. But when I try to send pass it on by replacing the 'expect eof' with

expect eof { send_user $expect_out(buffer) }

I get an error:

send: spawn id exp0 not open
while executing
"send_user $expect_out(buffer) "
invoked from within
"expect eof { send_user $expect_out(buffer) }"

I'm guessing that what has happened is that because expect's stdin has closed, is has invalidated exp0, thus cutting off access to stdout. I tried using 'puts' instead, but got this error:

error writing "stdout": bad file number
while executing
"puts -nonewline $expect_out(buffer) "
invoked from within
"expect eof { puts -nonewline $expect_out(buffer) }"

I'm stuck! How can I make this work? Or is there an example of how to accomplish this task somewhere?

Basically, at the end of the day, I want to be able to invoke

../script < input > output

and have it work the same way as

mysql -u user -ppass db < input > output

except that expect types the password rather than supplying it on the commandline (I don't want to supply the password on the commandline as it is insecure, and also not adaptable to programs other than mysql).

Any/all help will be appreciated!

Ben.
.



Relevant Pages

  • Trying to write a password auto-entry expect script
    ... I am trying to write a script to invoke mysql, automatically provide the password, and then give control to the user, terminating with the exit status of mysql. ... It seems that interact does not pass the eof through to mysql, ...
    (comp.lang.tcl)
  • Re: Moving through records and EOF
    ... I could not duplicate your issue with the .EOF code not firing, ... Private Sub Form_Open ... 'I think you want Dim mySQL as string, ...
    (microsoft.public.access.formscoding)
  • Re: expect + telnet hangs on AIX
    ... > EOF, ... wait occures implicitly on exit. ... It also hangs even if I misspel any command and it throws ...
    (comp.lang.tcl)
  • Re: query status of STDIN
    ... factually say that indeed eof is irrelevant to my question because it doesn't indicate the exit code of the process that might have created the stream. ... of my problem directly contradicts your understanding of my original ...
    (comp.lang.perl.misc)
  • Mysql CREATE FUNCTION libc arbitrary code execution.
    ... Mysql CREATE FUNCTION libc arbitrary code execution. ... by using on_exit, strcat and exit libc function we could ... strcat(UDF_INIT *initid, UDF_ARGS *args) ... So, by using as first argument a shellcode, mysql will execute it. ...
    (Bugtraq)