Expect can manipulate an interactive command, for example, input password when read certain string from output.
A very good "howto"
http://www.pantz.org/software/expect/expect_examples_and_tips.html
expect [[-opts] pat1 body1] ... [-opts] patn [bodyn]
expect {
busy {puts busy\n ; exp_continue}
failed abort
"invalid password" abort
timeout abort
connected
}
interact - return control to the user
send - Sends string to the current process.
send_error - sent to stderr
send_log - sent to the log file (see log_file.)
send_tty - send to /dev/tty
send_user- sent to stdout
When sending output with send_user, add \r\n to separate the text with command output...
A "SSH password tester"...
#!/usr/bin/expect -f
set timeout 20
set password [lindex $argv 0]
send_user "Trying $password...\r\n"
spawn ssh renbing@host.to.test
expect {
"password:" {
send_user "\r\nSending password\r\n"
send "$password\r"
expect {
"Last login" {send_user "\r\nYes password is correct\r\n"}
"Permission denied" {send_user "\r\nNo not right\r\n"}
eof {send_user "\r\nexpect password EOF\r\n"}
timeout {send_user "\r\nexpect password timeout\r\n"}
}
}
eof {send_user "\r\nexpect prompt EOF\r\n"}
timeout {send_user "\r\nexpect prompt timeout\r\n"}
}