0

I'm trying to write an expect script, the prompt I am expecting to handle is ade 123456789: / >. The number 123456789 will change, so I though I could just match on the only > I'm expecting to receive. I can't seem to get it to work however.

#!/usr/bin/expect

set timeout 3
set uuid [lindex $argv 0]
spawn telnet localhost 1402
expect ">"
send "/proc/OBRP/ObjectByUUID $uuid"

Calling the script with expect.sh my_uuid in debug gives me the following output. It seems to send the command but I don't see the output, whereas I do if run manually.

expect version 5.45.4
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = ./expect.sh  argv[3] = 1234ABCD-ABCD-123A-123A-123456ABCDEF  
set argc 1
set argv0 "./expect.sh"
set argv "1234ABCD-ABCD-123A-123A-123456ABCDEF"
executing commands from command file ./expect.sh
spawn telnet localhost 1402
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {1905}

expect: does "" (spawn_id exp4) match glob pattern "ade 123456789: / >"? no
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
ADE 4.1 - Console Ready.

ade 123456789: / > 
expect: does "Trying ::1...\r\nTrying 127.0.0.1...\r\nConnected to localhost.\r\nEscape character is '^]'.\r\nADE 4.1 - Console Ready.\r\n\u001b[K\r\nade 123456789: / > " (spawn_id exp4) match glob pattern "ade 123456789: / >"? yes
expect: set expect_out(0,string) "ade 123456789: / >"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Trying ::1...\r\nTrying 127.0.0.1...\r\nConnected to localhost.\r\nEscape character is '^]'.\r\nADE 4.1 - Console Ready.\r\n\u001b[K\r\nade 123456789: / >"
send: sending "/proc/OBRP/ObjectByUUID 1234ABCD-ABCD-123A-123A-123456ABCDEF" to { exp4 }
MattB
  • 135
  • 1
  • 3
  • 10
  • 2
    You need to add a carriage-return to the sent string: `send "...\r"`. – meuh Jan 20 '21 at 14:37
  • 1
    Also, your script is just falling off the end: even if you hit Enter as meuh suggests, you're not giving the telnet process a chance to respond. End the script with `interact` – glenn jackman Jan 20 '21 at 15:01
  • Thanks for the replies, I added `\r\n` and it did help. I didn't want to interact as I wanted to capture the next prompt and exit afterward. I do get the response now but then prompt looks different in the debug output than it does onthe console. ```ade 123456789: / > /proc/OBRP/ObjectByUUID 1234ABCD-ABCD-123A-123A-123456ABCDEF {"ERROR":""} ade 123456789: / > ``` ``` /proc/OBRP/ObjectByUUID 1234ABCD-ABCD-123A-123A-123456ABCDEF spawn id exp4 sent <{"ERROR":""}\u001b[K\r\n> {"ERROR":""} spawn id exp4 sent ade 123456789: / > > ``` – MattB Jan 20 '21 at 15:47

0 Answers0