【发布时间】:2019-10-16 23:57:01
【问题描述】:
对于以下脚本,expect 语言中的 if 块不起作用:
#!/usr/bin/expect -f
set timeout -10
set prompt {$}
spawn ssh 1.1.1.1
expect -re "$prompt"
send -- "echo 12\r"
expect {
"12" {send "echo hunky-dory\r"}
"bg" {send "echo 1\r"}
"ih" {send "echo 2\r"}
}
send -- "exit\r"
interact
代码将回显 12。但是,它不服从 if 块,因此不会在回显 12 之后回显 hunky-dory,正如我的脚本所明确指出的那样。
输出#!/usr/bin/expect -d:
expect version 5.45
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./testexpect.exp
set argc 0
set argv0 "./testexpect.exp"
set argv ""
executing commands from command file ./testexpect.exp
spawn ssh <hidden for security reasons>
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {3920}
send: sending "echo 12\r" to { exp4 }
expect: timed out
send: sending "exit\r" to { exp4 }
tty_raw_noecho: was raw = 0 echo = 1
spawn id exp4 sent <echo 12\r\nexit\r\n>
echo 12
exit
我的期望块甚至没有被识别。
【问题讨论】:
-
使用
expect -d运行您的脚本以查看更多调试信息。 -
@pynexj 我在 shebang 之后放了一个 d,我的 if 块甚至没有在任何地方注册,它只是回显 12 然后继续退出。而且我不能使用 sexpect,因为我正在尝试配置网络设备。
-
@pynexj,是的,我所说的 if 块是指 expect {} 块,因为它的功能类似于 if 块。
-
请使用
-d发布输出。 -
set prompt {$}; expect -re $prompt不起作用。单个字符 RE 模式$可以匹配一个空字符串,因此它会立即完成。