【问题标题】:Expect: how to print every output of a spawned process to the user期望:如何将生成的进程的每个输出打印给用户
【发布时间】:2011-10-29 15:45:16
【问题描述】:

我正在尝试编写一个期望脚本,部分命令将作为不同的用户执行。所以我需要生成一个 kuu 进程,然后在用户提供密码后向它发送命令。但是如何收集这些命令的输出并将其打印给运行期望脚本的用户?

谢谢

【问题讨论】:

    标签: tcl expect


    【解决方案1】:

    这可以通过以下方式来完成:

    proc outputUntilPrompt {} {   
        global expect_out
        set prompt "ACT:*>*"
        set output ""                
    
        while 1 {        
            expect {
                -re "(\[^\r]*\)\r\n" {
                    append output $expect_out(buffer)
                }
                $prompt {
                    append output $expect_out(buffer)
                    break
                }
            }
        }
        return $output
    }
    
    send_user "$output"
    

    【讨论】:

    • 使用append output $expect_out(buffer)而不是set output "$output$expect_out(buffer)";效果相同,执行速度更快。
    • 根据 Donal Fellows 的评论更新了代码以缩短执行时间。
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多