Tcl / Tk:在希望脚本

时间:2016-02-05 12:05:30

标签: tcl tk

我有一个愿望脚本,它会产生,期望和发送并获得所需的数据。 当我按照愿望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生成远程会话并获得所需的输出?

1 个答案:

答案 0 :(得分:4)

Tk 有一个send命令用于与其他启用Tk的解释器进行通信(它以与Expect完全不同的方式工作)并且两者相互作用。或者更确切地说,Expect在看到Tk时退出,并且不会创建send命令。

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

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