使用 expect 手动运行时不会发生重复响应

问题描述

我一直在尝试,with a little help 创建一个期望脚本来通过 telnet 查询服务。

有时收到的响应是在 expect 调试响应 [2] 中连接在一起的两个系统提示。手动完成后不显示[1],有人可以建议为什么会这样吗?

这是我用 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] 控制台输出

# 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 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 = 0

解决方法

如果您查看调试输出,您可以看到“>”命令提示符重复出现。这可能会导致您的脚本无法跟踪它应该做什么。

提示重复的原因是因为 send 命令以 \r\n 结尾。这些字符中的每一个都转换为换行符,因此就像发送 2 个命令,第二个是空行。

您应该仅以 \r 结束每个命令,这对应于键入 Enter。您返回的输出将包含 \r\n,这是正常的。