2
votes

I made an expect code:

#!/usr/bin/expect -f
spawn ls
expect "*]$*"
send "cd /to/some/path\n"
expect "*]$*"
send "sudo -u root ./program.sh"
expect "*:*"
send "i_am_password\n"
interact

while executing it I am getting the below error:

spawn ls
my_pgm.exp abc.sh axyz.zip all.zip test.exp
send: spawn id exp6 not open
while executing
"send "cd /to/some/path\n""
(file "./my_pgm.exp" line 5)

I am running this code on ubuntu. Please help.

1
Don't do this! It's terrible! - gniourf_gniourf
What you should do instead is update /etc/sudoers to allow you to sudo program.sh without having to enter your password. As a principle, never hard-code your password in a text file. - glenn jackman

1 Answers

1
votes

I'm not sure to really understand why you need expect, but for a bash script, try this:

#!/usr/bin/expect -f
spawn bash -i
expect "*$ "
send "cd /to/some/path\n"
expect "*$ "
send "sudo -u root ./program.sh\n"
expect "*: "
send "i_am_password\n"
interact

Description

The spawn directive instruct expect which program are to be used to interact with. So if you want to interact with bash, you have to ask expectto spawn bash. The -i parameter to bash enforce them to start in interactive mode.

At all, this look like a very bad idea, but that's only my opinion ;)