【问题标题】:Interact not working from within Expect Script在期望脚本中交互不起作用
【发布时间】:2021-03-30 23:53:12
【问题描述】:

我在 bash 脚本中有以下期望脚本,但我不确定交互命令为什么不起作用。

expect <<-EOS
    #!/usr/bin/expect
    set timeout $EXP_TIMEOUT
    send_user "\n The timeout being used is $EXP_TIMEOUT \n"
    send_user "\nLogging into remote host via SSH:\n"
    spawn ssh -q -o ConnectTimeout=$SSH_TIMEOUT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${hostname}
    expect "*assword*"
    send -- "$secret\r"
    expect {
            "*assword*" {
                    send \x03
                    puts "\nIncorrect Password\n"
            }
            "$prompt" {
                    send -- "/usr/seos/bin/sesu - $user\r"
                    expect "*assword*"
                    send -- "$secret\r"
                    expect "$prompt"
                    send -- "id\r"
                    expect "$prompt"
                    send -- "hostname -s\r"
                    interact
            }
    }
    expect eof

EOS 谢谢大家的帮助!

【问题讨论】:

  • 看看我的sexpect,你可以用它在shell代码中编写Expect all。

标签: bash tcl expect


【解决方案1】:

interact 不能让用户通过标准输入输入数据,因为您已经为此处的文档重定向标准输入。

相反,您可以将包含所有扩展的 here 文档保存到一个变量中,然后将其传递给 -c。这是一个简化的示例:

script=$(cat << EOF
    spawn vi
    send "iHello $(hostname)"
    interact
EOF
)
expect -c "$script"

【讨论】:

  • 这是一个复杂的地方,将期望代码放在自己的脚本文件中是一个好计划。不必一次处理多个级别的引用(同时使用 bash 和 tcl!),需要显式传递参数而不是通过替换来传递额外的痛苦是值得的。
猜你喜欢
  • 2017-12-09
  • 2013-02-26
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
相关资源
最近更新 更多