There are several posts regarding the same, but i still not able to make my expect script work properly. My intention is to automate everything but leave the password enter for
The interact
should be given with proper condition for the exit criteria.
The following script will execute the user commands in the shell
exeCmds.sh
#!/bin/bash
read -p "User: " user
echo "Expect entered the username $user"
read -p "Password: " pass
echo "User entered the password $pass"
while :
do
# Simply executing the user inputs in the shell
read -p "Shell> " command
$command
done
automateCmdsExec.exp
#!/usr/bin/expect
spawn ./exeCmds.sh
expect User
send dinesh\r
expect Password
send welcome!2E\r
expect Shell>
puts "\nUser can interact now..."
puts -nonewline "Type 'proceed' for the script to take over\nShell> "
while 1 {
interact "proceed" {puts "User interaction completed.";break}
}
puts "Script take over the control now.."
# Now, sending 'whoami' command from script to shell
send "whoami\r"
expect Shell>
# Your further code here...
The script automateCmdsExec.exp
will address the login needs of the bash script and when the prompt arrives, it will hand over the control to user.
We should define an exit criteria for the interact
for which I have used the word proceed
. (You can alter it as per your need).
Once interact
matched the word proceed
. it will return the control back to the expect script.
For demo purpose, I kept one more send
-expect
pair of command.
i.e.
send "whoami\r"
expect Shell>
You can keep your further code below the interact
, thus it can be executed by script.