【问题标题】:expect, interact and then again expect期待,互动,然后再次期待
【发布时间】:2016-02-24 14:05:05
【问题描述】:

有几个相同的帖子,但我仍然无法让我的期望脚本正常工作。我的意图是自动化一切,但为用户输入密码。所以脚本有3部分:

  1. 自动登录
  2. 让用户交互输入密码
  3. 将控制权交还给 Expect 脚本以继续工作

所以我有将生成的脚本,其中有 3 个读取命令。第一个和最后一个应该由 Expect 填写,第二个我想输入我自己:

#!/bin/ksh
read user?User:
echo "Expect entered the username $user"
read pass?Password:
echo "User entered the password $pass"
read command?"Shell>"
echo "Expect entered the command $command"

我的期望脚本:

#!/usr/bin/expect
spawn ./some_script
expect User
send I-am-expect\r
expect Password
interact
expect Shell
send I-am-expect-again

不幸的是,在我输入密码后,脚本无法继续并进入交互模式:

[root@localhost ~]# ./my-expect
spawn ./some_script
User:I-am-expect
Expect entered the username I-am-expect
Password:i am user
User entered the password i am user
Shell>

最后,当我在“Shell”上输入内容并按 [ENTER] 时,期望退出并出现错误:

Expect entered the command
expect: spawn id exp4 not open
    while executing
"expect Shell"
    (file "./my-expect" line 7)
[root@localhost ~]#

我感谢对此问题的任何解释或解决方案。我正在使用期望版本 5.45

【问题讨论】:

    标签: linux login passwords expect interaction


    【解决方案1】:

    interact 应该为退出标准提供适当的条件。

    以下脚本将在 shell 中执行用户命令

    exeCmds.sh

    #!/bin/bash
    read -p "User: " user
    echo "Expect entered the username $user"
    read -p "Password: " pass
    echo "User entered the password $pass"
    while :
    do
            # Simply executing the user inputs in the shell
            read -p "Shell> " command
            $command
    done
    

    automateCmdsExec.exp

    #!/usr/bin/expect 
    spawn ./exeCmds.sh
    expect User
    send dinesh\r
    expect Password
    send welcome!2E\r
    expect Shell>
    puts "\nUser can interact now..."
    puts -nonewline "Type 'proceed' for the script to take over\nShell> "
    while 1 {
            interact "proceed" {puts "User interaction completed.";break}
    }
    puts "Script take over the control now.."
    
    # Now, sending 'whoami' command from script to shell
    send "whoami\r"
    expect Shell>
    
    # Your further code here...
    

    脚本automateCmdsExec.exp 将解决bash 脚本的登录需求,当提示到达时,它将控制权交给用户。

    我们应该为interact 定义一个退出标准,为此我使用了proceed 这个词。 (您可以根据需要进行更改)。

    曾经interact 匹配单词proceed。它会将控件返回给期望脚本。

    出于演示目的,我又保留了一对send-expect 命令。

    send "whoami\r"
    expect Shell>
    

    您可以将更多代码保留在interact 下方,因此可以通过脚本执行。

    【讨论】:

    • 是的,但我想在输入密码后立即给予控制权。按下 Enter 键后立即。在原始场景中,我无法输入诸如“继续”或“开始”之类的内容来将控制权交还给期望。我提供的脚本只是我试图与之通信的 shell 的示例/模拟器。主要思想是让用户只输入一个密码,剩下的就由期望来完成。我不想在脚本中硬编码密码或通过参数传递它。
    • @PetrasL 我也有类似的需求。你终于找到解决办法了吗?
    • @PetrasL 如果您能通过更新您的问题或对您接受的答案添加评论来结束您的问题,我们将不胜感激,因为此处提供的答案并不能准确回答您的问题。
    【解决方案2】:

    您可以自己读取 (expect_user) 用户的密码,然后将 send 读取到生成的程序。例如:

    [STEP 101] # cat foo.exp
    proc expect_prompt {} \
    {
        global spawn_id
        expect -re {bash-[.0-9]+(#|\$)}
    }
    
    spawn ssh -t 127.0.0.1 bash --noprofile --norc
    expect "password: "
    
    stty -echo
    expect_user -timeout 3600 -re "(.*)\[\r\n]"
    stty echo
    send "$expect_out(1,string)\r"
    
    expect_prompt
    send "exit\r"
    expect eof
    [STEP 102] # expect foo.exp
    spawn ssh -t 127.0.0.1 bash --noprofile --norc
    root@127.0.0.1's password:
    bash-4.3# exit
    exit
    Connection to 127.0.0.1 closed.
    [STEP 103] #
    

    【讨论】:

    • 我注意到一个方括号似乎是“转义”,但另一个方括号却没有。这是故意的吗? (中途)\[\r\n]"
    • 左括号是特殊的 (tcl.tk/man/tcl8.6/TclCmd/Tcl.htm#M11) 但右括号不是。转义右括号并没有什么坏处,但不是绝对必要的。
    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多