Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
expect and multiple processes

Posted by mathias77 (mathias77), 18 November 2003
Hi,

I'm a rookie trying to learn expect
I have a question:
If I want my expect script to handle two processes at a time but in two different terminals how do I do that? I have tried by having the first process executing in the same terminal as I started the script and the second to work in a new terminal by spawning a new terminal and then saving the spawn_id. But when I want to send to this process I had no luck

Can anyone give a quick example that works involving a new terminal and sendin messages to both processes?

Regards,
Mathias

Posted by admin (Graham Ellis), 18 November 2003
I suspect that your problem could relate to the fact that spawn sets the spawn_id, and when you do your second spawn you overwrite it. Expect and send relate to the current spawn_id by default, and its up to you to save and restore the spawn_id into another variable if you want to handle multiple processes.    You might say "how old-fashioned".   Yes - if Expect was being written today, no doubt an OO interface would be used ....

Here's a sample piece of code from our Tcl Basics course (see http://www.wellho.net/course/tbfull.html ) that controls two processes.

Code:
#!/usr/bin/expect

# multiple processes through expect

log_user 0
spawn last
set lastspawn $spawn_id

# First spawned process - loops through lines back from "last"
##############################################################

while 1 {
set spawn_id $lastspawn
expect {
       "\n" {
               set rcvd $expect_out(buffer)
               }
       eof  {
               break
               }
       }
set user [lindex [split $rcvd] 0]

if ([info exists already($user)]) {
       # We have already looked up last for this user
       incr accesses($user)
} else {

# For each new user in "last", spawn a "finger
##############################################


spawn finger $user
set name "(system)"

while 1 {
       
expect {
       "\n" {
               set fgot $expect_out(buffer)
               regexp "Name: \(.*\)." $fgot all name
               }
       eof  {
               break
               }
       }
}
set name [string trim $name]
set rcvd [string trim $rcvd]
set already($user) $name
set userinfo($user) [string range $rcvd 39 end]
set accesses($user) 1

# End of "finger" loop
######################

}

# End of "last" loop
####################

}

# Display the results!

puts "Most Recent Access information"
puts "Username         in real life           count    last access details"
foreach user [array names already] {
       if {[string match *\[a-zA-Z0-9\]* $userinfo($user)]} {
       puts [format "%8s %30s %3d %s" $user $already($user) \
               $accesses($user) $userinfo($user)]
       }
       }


You may also like to note that the expect command can be instructed to wait for input from any one of a whole list of processes, using a list of process ids on which to wait and the -i option.   Do post further if you would like an example of this - we use it to parallel ping all our training computers to test the network, and it's great because we don't have to wait for timeouts on each individual machine that may not be responding.

Posted by mathias77 (mathias77), 18 November 2003
Ok,

thanks for your help!

Still I've got the problem with spawning a terminal and then trying to execute commands on the new terminal.

See the following example:

spawn dtterm -geometry 80x40+63+13 -title test -fg green -bg black
sleep 2
send "ls\r"
sleep 2

the 'ls' command is not executed in the new terminal.

Any idea on how to solve this?

Regards,
Mathias


Posted by admin (Graham Ellis), 19 November 2003
I'm more familiar with xterm, and I would tend to use the -e option to start the terminal off with a specific command.  `There's also a whole section on automatic xterm in Don Libes' book on expect - see
http://www.wellho.net/book/1-56592-090-2.html



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho