6

I have Ubuntu 9.10 (and cannot change it in the near future due to testing) with expect5.45. Expect is working, but I have (at least) two issues. The first is that expect does not seem to be matching the string "password:". On my machine, the manual interaction appears as below:

root@Monkey2:~# rlogin $remoteAddress -l root
root@nn.nn.nnn.nnn's password: 
Linux systest-desktop 2.6.31-22-generic #70-Ubuntu SMP Wed Dec 1 23:51:13 UTC 2010 i686

To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/

72 packages can be updated.
70 updates are security updates.

Last login: Mon Jun 20 13:40:22 2011 from monkey2.local
root@systest-desktop:~# 

The contents of the script is as follows:

#!/usr/local/bin/expect -f

##  Connect to a neighboring linux box.
spawn ssh nn.nn.nnn.nnn -l root

expect "password:\n"

send "mypassword\n"

send "$expect_out(0,string)"

The script gets and displays the line requesting the password but times out. I've tried using "password:", "password: ", "password:\r" and password:*" but none seem to match.


The second issue is that the expect_out variable is not recognized and I get the following error when I run the script (whose name is 'this' and only has 11 lines!):

can't read "expect_out(0,string)": no such variable while executing "send "$expect_out(0,string)"" (file "this" line 18)

Mike Loutris
  • 61
  • 1
  • 3

1 Answers1

8

First thing to do when debugging expect programs is to add this line near the top:

exp_internal 1

That turns on (verbose) debugging output that shows what expect has seen from the spawned program, and how it matches your expect patterns.

Typically, one does something like this:

expect -re {password: $}
send -- "the_password\r"

You need to send a \r (a carriage return) instead of \n.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank you Glenn. Magically, expect_out() is working, and with your suggestions, I'm getting matches. Thank you for your help! (BTW, I do have "Exploring Expect" by Don Libes, but I'm just into chapter 4 now. I'll need to look up the -- option for send .. – Mike Loutris Jun 21 '11 at 19:23
  • @Mike, good job getting "the" book. The "--" option is to explicitly signal the end of options for the command just in case the string you want to send begins with a hyphen. It's a good habit to get into, and will save your butt if you're dealing with user input and `send -- $user_input\r` – glenn jackman Jun 21 '11 at 19:48
  • Thanks, @glennjackman! Is this the book you guys are referring to: http://shop.oreilly.com/product/9781565920903.do – Till May 30 '12 at 18:52
  • This answer does not answer the second (part of the) question. – U. Windl Apr 21 '21 at 09:59