Tony Tony - 7 months ago 28
Bash Question

expect script not sending password correctly

This is the end goal: execute a mysql command using the expect script to send password when prompted. There are 3 files in total


  1. test.sh

  2. test1.sh

  3. test.exp



Here are the codes:


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


The result of me running expect test.exp is that I only see the prompt:

Enter password:


Any expert can lend me a hand to see what is going wrong? It certain means that test1.sh has run successfully, and also I have tried many times, including using just static password. Still just the prompt.

Thanks a bunch!

Answer

In order for a program to be interactable-with via expect and send, you need to launch it with spawn and not exec. Try this:

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