期望脚本没有正确发送密码

expect script not sending password correctly

这是最终目标:使用 expect 脚本执行 mysql 命令以在出现提示时发送密码。共有3个文件

  1. test.sh
  2. test1.sh
  3. test.exp

代码如下:

test.sh (returns the password that is needed to connect to mysql database)

grep hibernate.connection.password /opt/cisco/cpam/properties/vx.hibernate.properties | sed 's/hibernate.connection.password=//g'

test1.sh (the mysql command that needs to be excecuted)

mysql -u cpam -p --protocol=tcp vxdb < /opt/cisco/cpam/import/MySQL_Views.sql

test.exp (the expect script that sends password while executing mysql command)

set password [exec ./test.sh]
exec ./test1.sh
expect "Enter password: "
send  "$password\r"
expect eof

我运行ning expect test.exp的结果是只看到提示:

Enter password:

哪位高手可以帮我看看是哪里出了问题?这肯定意味着 test1.sh 已经 运行 成功了,而且我已经尝试了很多次,包括只使用静态密码。仍然只是提示。

非常感谢!

为了让程序可以通过 expectsend 进行交互,您需要使用 spawn 而不是 exec 来启动它。试试这个:

set password [exec ./test.sh]
spawn ./test1.sh
expect "Enter password: "
send  "$password\r"
expect eof