Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spawn from a variable in Expect

Tags:

expect

tcl

There are processes that take in a password partway through computation. I wish to insert a password at the start of the process, so I am trying to create a generic expect script that will spawn a process, then give it a password.

This expect script takes in the password to enter and the process to spawn:

#!/usr/bin/expect

set timeout 120

# Get program to spawn and password to send
set pswd [lindex $argv 0]
set program [lindex $argv 1]

spawn $program

expect "Password:"
send "$pswd\r"

interact

This will be called from a bash function:

function vup {
    echo -n "Enter Password: "
    read -s pswd
    echo
    expectPassword.exp $pswd "vagrant up"
}

But I get the error:

couldn't execute "vagrant up": no such file or directory
    while executing
"spawn [join $program]"

This script seems to work when I use it on a program without arguments. But I'm not sure how to make it work for one with a variable number of arguments.

like image 246
trotha01 Avatar asked Sep 01 '25 10:09

trotha01


1 Answers

One way is to use eval:

eval spawn $program
like image 99
konsolebox Avatar answered Sep 03 '25 09:09

konsolebox