Reputation: 155
I've been trying, with a little help to create an expect script to query a service via telnet.
Sometime the response is received with two system prompts concatenated together in the expect debug response [2]. This doesn't display when done manually[1], can anyone suggest why this is the case?
Here is the expect script, which I call with expect.sh UUID
#!/usr/bin/expect
set timeout 3
set uuid [lindex $argv 0]
spawn telnet localhost 1402
expect ">"
send "/proc/OBRP/ObjectByUUID $uuid\r\n"
expect ">"
sleep 1
send "exit\r\n"
interact
[1] Console output
# telnet localhost 1402
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
ADE 4.1 - Console Ready.
ade 123456789: / > /proc/OBRP/ObjectByUUID 12345678-0123-0123-0123-012345678912
{"ERROR":""}
ade 123456789: / >
[2] Expect debug output
expect version 5.45.4
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./expect.sh argv[3] = 12345678-1234-1234-1234-123456789012
set argc 1
set argv0 "./expect.sh"
set argv "12345678-1234-1234-1234-123456789012"
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 {30855}
expect: does "" (spawn_id exp4) match glob pattern ">"? 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 ">"? yes
expect: set expect_out(0,string) ">"
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 12345678-1234-1234-1234-123456789012\r\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern ">"? no
/proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n" (spawn_id exp4) match glob pattern ">"? no
{"ERROR":""}
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n{"ERROR":""}\u001b[K\r\n" (spawn_id exp4) match glob pattern ">"? no
ade 123456789: / > ade 123456789: / >
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n{"ERROR":""}\u001b[K\r\nade 123456789: / > ade 123456789: / > " (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n{"ERROR":""}\u001b[K\r\nade 123456789: / >"
send: sending "exit\r\n" to { exp4 }
tty_raw_noecho: was raw = 0 echo = 1
spawn id exp4 sent <exit\r\n\r\n\u001b[K\r\n>
exit
spawn id exp4 sent <Connection closed by foreign host.\r\n>
Connection closed by foreign host.
interact: received eof from spawn_id exp4
tty_set: raw = 0, echo = 1
tty_set: raw = 3, echo = 0
expect version 5.45.4
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./expect.sh argv[3] = 12345678-1234-1234-1234-123456789012
set argc 1
set argv0 "./expect.sh"
set argv "12345678-1234-1234-1234-123456789012"
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 {30865}
expect: does "" (spawn_id exp4) match glob pattern ">"? no
Trying ::1...
Trying 127.0.0.1...
expect: does "Trying ::1...\r\nTrying 127.0.0.1...\r\n" (spawn_id exp4) match glob pattern ">"? no
Connected to localhost.
Escape character is '^]'.
expect: does "Trying ::1...\r\nTrying 127.0.0.1...\r\nConnected to localhost.\r\nEscape character is '^]'.\r\n" (spawn_id exp4) match glob pattern ">"? no
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 ">"? yes
expect: set expect_out(0,string) ">"
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 12345678-1234-1234-1234-123456789012\r\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern ">"? no
/proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n" (spawn_id exp4) match glob pattern ">"? no
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n" (spawn_id exp4) match glob pattern ">"? no
{"ERROR":""}
ade 123456789: / > ade 123456789: / >
expect: does " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n{"ERROR":""}\u001b[K\r\nade 123456789: / > ade 123456789: / > " (spawn_id exp4) match glob pattern ">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " /proc/OBRP/ObjectByUUID 12345678-1234-1234-1234-123456789012\r\n\r\n\u001b[K\r\n{"ERROR":""}\u001b[K\r\nade 123456789: / >"
send: sending "exit\r\n" to { exp4 }
tty_raw_noecho: was raw = 0 echo = 1
spawn id exp4 sent <exit\r\n\r\n>
exit
spawn id exp4 sent <\u001b[K\r\n>
spawn id exp4 sent <Connection closed by foreign host.\r\n>
Connection closed by foreign host.
interact: received eof from spawn_id exp4
tty_set: raw = 0, echo = 1
tty_set: raw = 3, echo = 0
Upvotes: 0
Views: 154
Reputation: 12255
If you look at the debug output, you can see that the ">" command prompt is repeated. This can cause your script to lose track of what it should be doing.
The reason the prompt is repeated is because the send
commands end with \r\n
. Each of these characters is converted to a newline, so it is like sending 2 commands, the second one being an empty line.
You should end each command with just \r
, which corresponds to typing Enter. The output you get back will have \r\n
, which is normal.
Upvotes: 1