Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make expect command in expect program script to wait for exact string matching

I want to know how to make expect command in expect script to wait for exact string to be matched before proceeding to next line in the script.

Bourne shell (sh) script:

#!/bin/sh
#!/usr/bin/expect

spawn ssh -Y localuser@lbblr-tirumala
expect -exact "localuser@lbblr-tirumala's password: "

# replace password with ur own passwd
send "geomax45\r"

# replace the prompt with ur own prompt
expect "localuser@lbblr-tirumala:~$ "

# run application
send "./sample\r"

expect "*Menu*\r
1. print hello world\r
2. print Bye world\r
3. quit\r
enter choice: "
# enter choice 1
send "1\r"
expect "Bye world\r
*Menu*\r
1. print hello world\r
2. print Bye world\r
3. quit\r
enter choice: $"
# enter choice 2
send "2\r"

In the above code after entering 1 the code should wait for "Bye world\r......" string to occur on the screen. But even though the screen prompts different string altogether, the code is executing the next line and entering option 2, which should not occur as per definition of expect. It should wait until the complete string in expect command matches.

I want to know whether there is any stricter command that can be used so that expect waits until exact string matching occurs. Please help me in this issue

like image 708
chaitu Avatar asked Apr 09 '12 10:04

chaitu


People also ask

What is timeout in expect script?

By default, the expect timeout is 10 seconds. If you don't enter anything for the expect command, it times out in 20 seconds.

How do you run a command in expect script?

The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout. Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.

How does expect command work?

The Linux expect command takes script writing to an entirely new level. Instead of automating processes, it automates running and responding to other scripts. In other words, you can write a script that asks how you are and then create an expect script that both runs it and tells it that you're ok.

What is Exp_continue in expect?

The command exp_continue allows expect itself to continue executing rather than returning as it normally would. By default exp_continue resets the timeout timer. The -continue_timer flag prevents timer from being restarted. (See expect for more information.)


1 Answers

You're already using it earlier in your script: expect -exact. But long matching strings are pretty much guaranteed to cause problems; you should try to cut it down to the shortest unique match, if necessary breaking it into multiple expect commands.

In this case, just based on what you show, the proper command is probably expect -exact "choice:". But inherently it is not possible to determine this without full details of the possible program outputs.

like image 65
geekosaur Avatar answered Sep 27 '22 19:09

geekosaur