【问题标题】:linux expect language - script not working for if statementlinux期望语言-脚本不适用于if语句
【发布时间】: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 模式 $ 可以匹配一个空字符串,因此它会立即完成。

标签: linux expect


【解决方案1】:

你的问题是这样的:

set timeout -10

expect 将该负值视为零。实际上没有时间分配给expect 块来行动,你直接去send exit

使用 -1 或大于零的某个值作为超时值。

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多