简体   繁体   中英

Using expect multiple times in /usr/bin/expect

I'm trying to automate some calls from a message oriented middleware application called disque using disque and I'm running into some difficulty...

Everytime I want the client to run a command, I must expect the prompt it gives and send the command, but when I try to do it more than once, I run into problems.

Here is the expect script in its most basic non-functioning form:

set timeout 20


spawn "./disque"

set timeout 10
expect  "127.0.0.1:7711> "  {send "ADDJOB scriptqueue body 0\r"}
set timeout 10
expect  "127.0.0.1:7711> "  {send "GETJOB FROM scriptqueue\r"}

However, when I run the above script, only the first prompt is read and the first message is sent. The GETJOB command never is run.

Here is the output I get:

spawn ./disque
127.0.0.1:7711> ADDJOB scriptqueue body 0

Also as a reference, this is how disque works normally when I manually input those two strings when I'm prompted:

127.0.0.1:7711> ADDJOB scriptqueue body 0
DI530404a85743c0afe1353ae1943df8d86c9f561005a0SQ
127.0.0.1:7711> GETJOB FROM scriptqueue
1) 1) "scriptqueue"
   2) "DI530404a806ff88886c366ba3566d7e36ea81f89605a0SQ"
   3) "body"
127.0.0.1:7711>

UPDATE:

I just ran the script using the -d for debug mode and got this output:

expect version 5.45
argv[0] = expect  argv[1] = -d  argv[2] = cng4.sh
set argc 0
set argv0 "cng4.sh"
set argv ""
executing commands from command file cng4.sh
spawn ./disque
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {17657}

expect: does "" (spawn_id exp4) match glob pattern "*>"? no
127.0.0.1:7711>
expect: does "127.0.0.1:7711> " (spawn_id exp4) match glob pattern "*>"? yes
expect: set expect_out(0,string) "127.0.0.1:7711>"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "127.0.0.1:7711>"
send: sending "ADDJOB scriptqueue body 0\r" to { exp4 }

expect: does " " (spawn_id exp4) match glob pattern "*>"? no
127.0.0.1:7711> ADDJOB scriptqueue body 0
DI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ
127.0.0.1:7711>
expect: does " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711> " (spawn_id exp4) match glob pattern "*>"? yes
expect: set expect_out(0,string) " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711>"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\u001b[0K127.0.0.1:7711> A\r\u001b[17C\r\u001b[0K127.0.0.1:7711> AD\r\u001b[18C\r\u001b[0K127.0.0.1:7711> ADD\r\u001b[19C\r\u001b[0K127.0.0.1:7711> ADDJ\r\u001b[20C\r\u001b[0K127.0.0.1:7711> ADDJO\r\u001b[21C\r\u001b[0K127.0.0.1:7711> ADDJOB\r\u001b[22C\r\u001b[0K127.0.0.1:7711> ADDJOB \r\u001b[23C\r\u001b[0K127.0.0.1:7711> ADDJOB s\r\u001b[24C\r\u001b[0K127.0.0.1:7711> ADDJOB sc\r\u001b[25C\r\u001b[0K127.0.0.1:7711> ADDJOB scr\r\u001b[26C\r\u001b[0K127.0.0.1:7711> ADDJOB scri\r\u001b[27C\r\u001b[0K127.0.0.1:7711> ADDJOB scrip\r\u001b[28C\r\u001b[0K127.0.0.1:7711> ADDJOB script\r\u001b[29C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptq\r\u001b[30C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqu\r\u001b[31C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptque\r\u001b[32C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueu\r\u001b[33C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue\r\u001b[34C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue \r\u001b[35C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue b\r\u001b[36C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bo\r\u001b[37C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue bod\r\u001b[38C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body\r\u001b[39C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body \r\u001b[40C\r\u001b[0K127.0.0.1:7711> ADDJOB scriptqueue body 0\r\u001b[41C\r\nDI530404a8efd0d77b5bb62a7568510f7b6a29a13405a0SQ\r\n127.0.0.1:7711>"
send: sending "GETJOB FROM scriptqueue\r" to { exp4 }

It looks like the reason is that it's picking up some other output from the disque application itself...I'm still not sure how to solve this.

Thanks.

As you can see from the debug log, GETJOB was really sent to the application. The problem is you did not wait for GETJOB to finish. Just think how do you know the GETJOB has finished when you manually interact with the application? You wait for the next prompt, right? So just tell expect to do the same thing. That's to say, tell expect to wait for the next prompt before it moves on (or exit).

expect "127.0.0.1:7711> " {send "GETJOB FROM scriptqueue\r"}
expect "127.0.0.1:7711> "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM