Riaan's SysAdmin Blog

My tips, howtos, gotchas, snippets and stuff. Use at your own risk!

BashExpect

Expect and bash

Quick example of spawning a problem that have a password prompt and can't accept a documented parameter for a password. I used another bash script to simulate a password prompt but in my automation challenge it was an executable that prompted.

Main script just to take a password for passing to expect. Could also be hard coded.

$ cat expect_example_main.sh
#!/bin/bash
echo "Enter the password: "
read -s -e password
./expect_example.exp $password ;

Here is the expect script that will be interacting with the real program with the password prompt.

$ cat expect_example.exp
#!/usr/bin/expect -f

# Set variables
set password [lindex $argv 0]
set date [exec date +%F]

# Log results
log_file -a expect-$date.log

# Announce device & time
send_user "\n"
send_user ">>>>> Working @ [exec date] <<<<<\n" send_user "\n" spawn ./expect_example_prompt.sh expect "*assword:" {send "$password\r"} interact This is the simulated executable with the prompt. Expect will be spawning this one. $ cat expect_example_prompt.sh #!/bin/bash echo "Enter the password: " read -s -e pwd if [ $pwd == 'fool' ]; then echo "password correct" else echo "password NOT correct!" fi Showing run time with correct password. $ ./expect_example_main.sh Enter the password: >>>>> Working @ Thu Jan 19 14:54:00 CST 2017 <<<<< spawn ./expect_example_prompt.sh Enter the password: password correct Showing run time with incorrect password. $ ./expect_example_main.sh Enter the password: >>>>> Working @ Thu Jan 19 14:54:28 CST 2017 <<<<< spawn ./expect_example_prompt.sh Enter the password: password NOT correct!

admin

Bio Info for Riaan