Tcl/Tk : 在 wish 脚本中嵌入 expect 发送脚本

Tcl/Tk : embed expect send script inside wish script

我有一个 wish 脚本,它会执行 spawn、expect 和 send 并获取所需的数据。 当我 运行 脚本如愿 ex.tcl 时,它不起作用。

请在下面找到代码以获取更多信息。

#!/usr/bin/wish

package require Expect

proc openSession { targetHost } {
    log_user 1

    set user $::env(USER)
    set password "tmp1234"
    set timeout 60
    set spawn_id ""

    puts "Ssh to $user@$targetHost"
    spawn ssh -o UserKnownHostsFile=/dev/null $user@$targetHost
    match_max -i $spawn_id 99999
    expect {
        "\(yes\/no\)\? " { send "yes\r" ; exp_continue }
        "password\:" { puts 2 ; send "$password\r" ; exp_continue }
        "$ " { puts 3 ; send "\r" ; puts "Ssh Session ready" }
        timeout { puts 4 ; set spawn_id "" ; puts "Timeout during ssh $targetHost" }
    }
    set timeout 10
    return $spawn_id
}

proc closeSession { sshSess args } {
    puts "Closing Ssh session..."
    expect -i $sshSess "$ " { send -i $sshSess "exit\r" }
    expect -i $sshSess "closed." { puts "Connection Closed" }
    catch { exp_close -i $sshSess }
    catch { exp_wait -i $sshSess }
    return
}


set sessId [openSession testbng76]
grid [ttk::button .mybutton -text Mytext]
closeSession $sessId

看到的错误:

Are you sure you want to continue connecting (yes/no)? Error in startup script: wrong # args: should be "send ?options? interpName arg ?arg ...?"
    while executing
"send "yes\r" "
    invoked from within
"expect {
        "\(yes\/no\)\? " { send "yes\r" ; exp_continue }
        "password\:" { puts 2 ; send "$password\r" ; exp_continue }
        "$ " { p..."
    (procedure "openSession" line 12)
    invoked from within
"openSession testbng76"
    invoked from within
"set sessId [openSession testbng76]"
    (file "./log.tcl" line 36)

如何生成远程会话并使用 tk 获得所需的输出?

Tk also 有一个 send 命令用于与其他支持 Tk 的解释器对话(它的工作方式与 Expect 完全不同)并且两者相互作用。或者更确切地说,Expect 在看到 Tk 时退缩,并且不创建 send 命令。

幸运的是,您可以使用 exp_send 代替;它正是您想要的功能的另一个名称,而 Expect 总是会创建它。例如:

    "\(yes\/no\)\? " { exp_send "yes\r" ; exp_continue }