Tony Tony - 2 years ago 108
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



  3. test.exp

Here are the codes: (returns the password that is needed to connect to mysql database)

grep hibernate.connection.password /opt/cisco/cpam/properties/ | sed 's/hibernate.connection.password=//g' (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 ./]
exec ./
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 has run successfully, and also I have tried many times, including using just static password. Still just the prompt.

Thanks a bunch!

Answer Source

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 ./]
spawn ./
expect "Enter password: "
send  "$password\r"
expect eof
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download