1
votes

I have a wish script which will do spawn, expect and send and get the required data. When I run the script as wish ex.tcl, its not working.

Please find the code below for more information.

#!/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

Error seen:

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)

How can I spawn the remote session and get the required output using tk?

1
Nice job hanging on to the spawn_id. You could also declare that variable as global in the openSession and closeSession procs.glenn jackman

1 Answers

4
votes

Tk also has a send command for talking to other Tk-enabled interpreters (it works in a totally different way to Expect) and the two interact. Or rather Expect backs off when it sees Tk, and doesn't create a send command.

Fortunately, you can use exp_send instead; it's another name for exactly the functionality you want, and Expect always creates it. For example:

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