expect - 手动运行时不会发生使用期望的重复响应
问题描述
我一直在尝试,在一些帮助下创建一个期望脚本来通过 telnet 查询服务。
有时会收到响应,其中两个系统提示在预期调试响应 [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 = 1
tty_set: raw = 3, echo = 0
解决方案
如果查看调试输出,您可以看到重复“>”命令提示符。这可能会导致您的脚本忘记它应该做什么。
重复提示的原因是send
命令以 . 结尾\r\n
。这些字符中的每一个都转换为换行符,因此就像发送 2 个命令,第二个是空行。
您应该以 just 结束每个命令\r
,这对应于键入Enter。你得到的输出会有\r\n
,这是正常的。
推荐阅读
- python - Django:按多个ID过滤查询集
- java - 单击停止按钮后 TimerTask 没有停止
- swift - 在计算属性中设置新值
- r - R识别数据框中的第一个值并通过从新列的数据框中的所有值中添加/减去该值来创建新变量
- python - 如何从 Django 中的项目级包导入而不与同名的应用级模块冲突?
- ruby-on-rails-5 - Rails5 API + devise_token_auth 编辑令牌格式
- .net - foreach(模型中的变量项),对象引用未设置为对象的实例
- css - 弹性柱上的多重对齐
- javascript - 通过 Ajax 上传 ExcelFile 并在 Controller 上读取
- ms-access - 连续表格高度 - 太高