3

I am using Ubuntu 14.04 and installed expect. I am trying to write a script to enter password when it prompted.
UPDATED Code:

  #!/usr/bin/expect -d
  set timeout 20

  set pw odroid
  spawn sudo apt-get update

  expect {\[sudo]\ password for odroid: }
  send "$pw\r"

  close

Any suggestions? thx

UPDATE Errors:

expect: does "" (spawn_id exp4) match glob pattern "\[sudo]\ password for odroid: "? no
[sudo] password for odroid: 
expect: does "[sudo] password for odroid: " (spawn_id exp4) match glob pattern "\[sudo]\ password for odroid: "? yes
expect: set expect_out(0,string) "[sudo] password for odroid: "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "[sudo] password for odroid: "
send: sending "odroid\r" to { exp4 }

3 Answers 3

2

A more generic way of solving your issue would be matching on assword

expect "*?assword*"

This allows for a more generic script where the username is irrelevant.

I also had the following issue with the snippet below. Basically $pw gets interpreted by the shell and needs to be set outside the expect TCL code.

pw="Password1234"

expect -f - <<-EOF
  set timeout 10

  spawn sudo -k
  spawn sudo mysql_secure_installation
  expect "*?assword*"
  send -- "$pw\r"
  expect eof
EOF
Sign up to request clarification or add additional context in comments.

Comments

0

You have too many quotes. Choose one of:

expect {\[sudo\] password for odroid: }
expect "\\\[sudo\\\] password for odroid: "

Clearly the first option is better.

Lots of escaping is necessary because 1) square brackets are special in a glob pattern, and 2) square brackets are special Tcl syntax (command substitution) in double quotes but not in curly braces.

3 Comments

Hi glenn, I changed as you said and this problems fixed. but still don't work. I add new output in my problem. please give me some suggestions. thx
Did you look at that debug output? The pattern did match and the password was sent. Whatever the problem is, it's not in what you've shown us.
but the updated informations are the output I have. How can I display the problem I have besides include -d in the script. thx
0

Try to replace with this, it should work fine.

expect "\[sudo\] password for odroid:\r"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.