This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Expect script could be improved

Posted by: Anonymous [ip: 199.172.169.7] on September 26, 2007 05:20 PM
It's great that it solved your problem, but this Expect script could be improved.
The option -re does not mean read, it means to treat the next thing as a Regular Expression when matching against the output from the spawned program. Since you're only matching a fixed string, -re is redundant here.
The second match line is also redundant, because the clauses within an expect statement will always be matched in order and since the patterns are the same the second will never be reached. The exp_continue means to restart the same expect statement again.
Also Expect is capable of doing the whole job, without needing a shell script or gawk. Here's an (untested) example:

#!/usr/bin/expect -f
foreach {name pass} [read [open passwords.txt]] {
spawn smbpasswd -a $name
expect {
"password:" {sleep 1; send "$password\r"; exp_continue}
}
}

For more docs see http://aspn.activestate.com/ASPN/docs/ActiveTcl/8.4/expect/expect.1.html

#

Return to Automated user management with Expect