Re: Trying to write a password auto-entry expect script
- From: Ben Schmidt <mail_ben_schmidt@xxxxxxxxxxxx>
- Date: Wed, 31 May 2006 14:52:50 +1000
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.
.
- Follow-Ups:
- Re: Trying to write a password auto-entry expect script
- From: Ben Schmidt
- Re: Trying to write a password auto-entry expect script
- References:
- Trying to write a password auto-entry expect script
- From: Ben Schmidt
- Trying to write a password auto-entry expect script
- Prev by Date: Trying to write a password auto-entry expect script
- Next by Date: Re: grabbing a hex data stream
- Previous by thread: Trying to write a password auto-entry expect script
- Next by thread: Re: Trying to write a password auto-entry expect script
- Index(es):
Relevant Pages
|
|