Search code examples
tclexpect

handling multiple processes in tcl/expect


I am trying to deal with two processes, which have to run simultaneously in expect. However, I keep getting the message that one of those processes does not exist.

Here is a minimal (not) working example (I am not really working with ftp, but thats something that will run for other people):

#!/usr/bin/expect

set spawn_id_bash [spawn /bin/bash]
set spawn_id_ftp [spawn ftp ftp.ccc.de]

send "anonymous\n"
expect {
    "*Password*" {
            puts "\nftp works"
    }
    default {
            puts "\nftp defaulted"
    }
}

set spawn_id $spawn_id_bash
send "uname\n"
expect {
    "*Linux*" {
            puts "\nbash works"
    }
    default {
            puts "\nbash defaulted"
    }
}

Unfortunately, the output is:

[martin@martin linuxhome]$ /tmp/blub.tcl 
spawn /bin/bash
spawn ftp ftp.ccc.de
anonymous
Trying 212.201.68.160...
Connected to ftp.ccc.de (212.201.68.160).
220-+-+-+-+-+-+-+-+-+
220-|o|b|s|o|l|e|t|e|
220-+-+-+-+-+-+-+-+-+
220-
220-
220-Please use HTTP instead:
220-
220-* http://cdn.media.ccc.de
220 
Name (ftp.ccc.de:martin): 331 Please specify the password.
Password:ftp works
can not find channel named "4648"
while executing
"send "uname\n""
(file "/tmp/blub.tcl" line 19)

I have followed the book "Exploring Expect" while writing this example and I do not see what I do differently.

I also tried using send -i and expect -i without any luck (the error message is gone, but otherwise -i seems to be ignored).


Solution

  • spawn returns the unix process id (PID, an integer), not the spawn_id (a string). For example:

    # cat foo.exp
    send_user "[spawn -noecho sleep 1] $spawn_id\n"
    expect eof
    # expect foo.exp
    20039 exp6
    #
    

    You should write like this:

    spawn /bin/bash
    set spawn_id_bash $spawn_id
    
    spawn ftp ftp.ccc.de
    set spawn_id_ftp $spawn_id
    

    Then you can use expect -i and send -i.