2

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 }
crazymumu
  • 101
  • 2
  • 10

3 Answers3

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
Aaron D. Marasco
  • 6,506
  • 3
  • 26
  • 39
Steve Clement
  • 83
  • 1
  • 11
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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 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 – crazymumu Jul 20 '16 at 01:40
  • 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. – glenn jackman Jul 20 '16 at 02:43
  • but the updated informations are the output I have. How can I display the problem I have besides include `-d` in the script. thx – crazymumu Jul 20 '16 at 03:51
0

Try to replace with this, it should work fine.

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

Itchydon
  • 2,572
  • 6
  • 19
  • 33
Basak
  • 11
  • 1