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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Questions about expect

Posted by ffaridi1 (ffaridi1), 17 March 2005
I have couple of questions:
1- I have activestate expect installed and am spawning a telnet process from my TCL script ( running on Win2K) , its working fine, but in my script when I call a function and if I have expect/send pairs in there looks like it lost control of telnet process at all, can you give me and example in which
- you spawn a telnet process
- do a expect/send pair
- call a fuction and inside that function do some more expect/sends. ?

2- I don't wanna pay for activestate expect and want to use the free one, but it can only work with tcl8.0 which is not ufficient for me, can you tell me how can I get the best of both worlds ( free expect and tcl8.4)?

Thanks and BTW terrific forum and answers !!!!

Posted by admin (Graham Ellis), 17 March 2005
Answering the second question first - you can get expect that's both free and up to date with the current Tcl if you use Linux or a Unix based operating system. It's only if you add the third specification "and I must use windows" that your choice becomes limited.

I've never had any issues with sends and expects in procs - though I don't think I have an example of exactly what you're looking for.  You do need to remember variable scope, and that you may need to pass things in and out of the procs or declare a number of your variable to be global;  that's often the area that has to be corrected when problems like this are reported.

Posted by ffaridi1 (ffaridi1), 17 March 2005
Thanks for the quick response

For expect commands to execute in some other proc other than the one in which I had spawned the telnet task, do I have to declare any thing global ( like $spawn_id or any thing els?) or pass in some thing?

- also for first question I read a previous thread in which that guy include expect PATH in windows PATH variable and use TCL8.4 for other tcl functions , so is that a way out?

Posted by admin (Graham Ellis), 17 March 2005
I've taken one of our demo scripts that uses expect to run multiple parallel pings and put the spawn and expect commands into separate procs.   I made the variables that have to be shared global, and it worked fine.

Here's the script (modified as described above)


# check a number of systems for signs of life at the same time

log_user 0
set timeout 60
set runningcount 0

# spawn out a series of pings


proc startpings {} {
global argv
global idlist
global hostname
global runningcount
foreach system $argv {
       spawn ping -c 3 $system
       lappend idlist $spawn_id
       set hostname($spawn_id) $system
       incr runningcount


proc awaitpings {} {
global idlist
global hostname
global runningcount
global ans

# gather up a series of responses

while {$runningcount > 0} {

expect {
       -i $idlist "\n" {
               set which $expect_out(spawn_id)
               lappend ans($which) $expect_out(buffer)
       eof {
               # search and remove completed host from idlist
               set donepos [lsearch $idlist $expect_out(spawn_id)]
               set idlist [lreplace $idlist $donepos $donepos]
               incr runningcount -1
       timeout {




# Run tests and display the results!


puts "Live systems test"

foreach which [array names ans] {
       set count "xx"
       foreach line $ans($which) {
               regexp "\(\[0-9\]\) (packets )?received" $line all count
       switch $count {
               xx {
                       puts "$hostname($which) - unable to ping (bad hostname)"
               0 {
                       puts "$hostname($which) - NOT RESPONDING"
               default {
                       puts "$hostname($which) - $count packets received"

The original that I modified to produce that is at Basically, the additions were the proc structures and the global declarations.

Here's an example of how it runs.

[trainee@pasanda trainee]$ ./clpsubs prawn pasanda buttercup cowslip coffee
Live systems test
cowslip - 3 packets received
coffee - 3 packets received
pasanda - 3 packets received
buttercup - 3 packets received
[trainee@pasanda trainee]$

I have no experience of mixing parts of different Tcl releases in the way you mention, though others might have done.  My advise would be to purchase the license if ActiveState Windows Expect provide what you need - after all, if you're using Windows you're already used to paying for software

Posted by ffaridi1 (ffaridi1), 17 March 2005
Can you tell me the biggest difference between tcl8.0 and tcl 8.3 because I think I can live with tcl8.0

As for spawn_id, when I spawn the task by:

spawn telnet $Ip
puts "----> $spawn_id \n"

it prints exp_spawn0 which I don't think is a valid id. Do you have any idea why?

Posted by admin (Graham Ellis), 18 March 2005
The biggest change that I can recall to Tcl from 8.0 onwards was the addition of enhanced regular expressions. Also internationalisation and Unicode added - a major thing for a few users.   The other difference is that for 8.3 or 8.4 there are people out there who'll be able to help you ... for 8.0 you'll get a number of "don't know ... wht are you still using that version" questions.    There's detail in - and I thinks there's a new edition of that out now ...

File handles are like objec references - you really shouldn't be printing them out - just using them in commands where a file handle is required.   Goodness only know what values they contain, except that it has to be a string as in Tcl everythiing is a string.   I haven't a clue if yours is right, but I expect it is.

Posted by MarkTChristy (MarkTChristy), 18 March 2005
on 03/17/05 at 07:44:25, ffaridi1 wrote:
2- I don't wanna pay for activestate expect and want to use the free one, but it can only work with tcl8.0 which is not ufficient for me, can you tell me how can I get the best of both worlds ( free expect and tcl8.4)?

I first will apologize   for my post! But I was so excited to see someone else was actually trying to use Expect (the free version). So, I apologize.  

I cannot get even simple spawns, let alone scripts to run at all under Windows2000 using the free version. If you did, can you please explain to me HOW   !

You can email me off the forum and we can talk about it. Email me at I watch this forum quite often but I don't want to louse up your nice thread.  

Posted by ffaridi1 (ffaridi1), 18 March 2005
Actually thats fine , (if moderators allow) we can discuss that here too so that others could get benifit from it too.

Actually I AM able to run free version of expect with Tcl8.0 but the only reason I wanna mix match it with Tcl8.4 is after I run my old scripts ( which run fine under Tcl8.4) I ran into some incompatibalities..... I resolve few of them by doing some work arounds but they keep on coming, and finally I thought why not find a way to use "Best of both worlds" and there I face a deadlock (atleast for now)

If you wanna use the Free expect with Tcl8.0 you have to make sure one tiny thing which is actually very unnoticeable and which will stop you right away and that is :

use "package require expect" instead of
    "package require Expect"  

Looks funny .... but believe me you HAVE to use "expect" instead of "Expect", rest of it is same.....

try using it and let me know if it works.

As for mixing TCL8.4 with free expect, I tried following:
- install tcl 8.4 in different folder and expect in different and include the paths to libraries in System/User environment variables using TCL_LIBRARAY and TCLLIBPATH , but it complains about package.

- Then I install expect in the same directory as TCL8.4 , this time it didnot complain about package but wish84 crashes.

- Then I again install tcl8.4 and free expect in separate folders and just copy library files and folders from expect directory to Tcl directory .... this time it didnot complain about package but some weired "dll" namespace error comes in.

For now I'm using eval version of Activestate expect with TCL8.4 , and I'm hopeful that soon there will be a solution which can allow me to use tcl8.4 with free expect rather than paying for it

Feel free to share your thoughts and BTW very informative forum for us TCL/TK programmers

Posted by admin (Graham Ellis), 19 March 2005
I'm perfectly happy for you to have a more general discussion on Expect for Windows here, provided that it stick within the forum agreement that you made when sigining up. There's a copy of the agreement available in case you wish to refer.  Indeed ... I'm going to join in with a comment of my own ...

I love the approach / philosophy of Open Source, and it's something of a regret to me that Expect running under Windows,  at recent versions, is now only available under a paid commercial license.  I haven't investigated the how / why / where commercially and legally - just don't have enough hours in the day, nor the contacts nor the knowledge.

I use Expect "in anger" on Linux and Unix based boxes; one of the scripts that I've written is a fundamental key as I set up a training course and runs parallel pings reported back to a Tk display - a great network tester that can feel the whole network quickly even when I'm on site with perhaps half my machines not present.  I've not put nor attempted to put the script onto Windows; I'm not tempted by having to run an older version, nor by licensing up what could be a whole fleet of machines and in any case I always have at least one *nix box present.

I advocate that if you want to use the commercial version, then you should either pay the money for it or find an alternative; you should NOT try to "fool" the system or break any license. In most cases a single license fee is going to be "small beer" in the overall cost of a project anyway.  We're a bit of an exception on that; Expect for Windows would be an occasional use thing and  I don't wish to justify the cost so I'll do what I need another way. Alternatives used / explored:
a) Host the script on a *nix platform (taken)
b) Write code at a lower level (I would use Perl sockets I expect)

i) In case you're wondering, I understand that the structure of the Windows Operating systems means that it's not at all easy to implement Expect well and reliably on that OS.  That's why the ActiveState people have a niche that they can charge for and no-one else has jumped in a with a modern version at no charge
ii) The multiple parallel ping script that I refer to is available in source code form on this site.

Posted by dvs_reddy (dvs_reddy), 27 April 2005

I have question with expect. I need to telnet to a device and excute couple of commands one after the other in a sequence. I noticied following strange behavior, whn executed the first command, output it sent is the same command that got executed and on executing the second command i see the output  of first command.  With the help of debug enabled i see the above scenario

Can you kindly help me why is this happening, what I need do?

Thanks for the help ,

Posted by admin (Graham Ellis), 27 April 2005
Hi, Vijay ... if you could give us a few lines of code, we might be able to advise on why they're not working - have a look at our help and assistance board for tips on finr tuning your question and giving us enough to help you.

-- Graham

Posted by mansoorx (mansoorx), 27 February 2006

     I would like to know how can I take an ip address and username and password as a variable in Expect Script, e.g.

#!/usr/bin/expect -f
spawn  telnet $1
expect "Username:"
send "$2\r"
expect "Password"
send "$3\r"

This script is called in another Script, e.g

/home/mansoorx/script   name  password

three Vriable  IP, Name, Password goes to Script File.
but it is not working

Please Guide me

Posted by admin (Graham Ellis), 27 February 2006
argv is a list of command line parameters.  So ..

replace $1 by [lindex $argv 0]
replace $2 by [lindex $argv 1]
and so on.

By the way - if you start a new thread when you ask a question, it'll get noticed a bit quicker and more people will read it.  Thanks.

Posted by sharmila (sharmila), 27 February 2006
Reply to mansoor

Create a script file called first and do insert the following lines.

proc telnet {ip login password } {
 spawn telnet $ip
 set telnet_id $spawn_id
 expect "*#"
 send "$login\r"
 send "$password\r"
 expect "*#"

Create another script file named second and do insert the foll.lines

source ../first(full path of the file here the name of the file which has the telnet procedure is first)
telnet $ip $login $password

I think this will start the process in the device whose address is $ip.

Posted by mansoorx (mansoorx), 28 February 2006
Thank You,
Graham, and thank you Sharmila,
it start to work


Posted by admin (Graham Ellis), 28 February 2006
Mansoorx, I don't know your Port command, nor do I know what paremeters it takes, what your passing into it, or what error message it is reporting.  I'm also somwhat unclear as to what the comment lines at the bottom are.

If you can post the actual error messages that you're getting, together with details of the arguments you're using, We may be able to help you if the problem lies in the Tcl.  With the information given so far, though, I'm just scratching my head and guessing!

Posted by mansoorx (mansoorx), 28 February 2006
Befor using  [lindex $argv 1]  i am using echo command to take input but after ur suggestion i start to use [lindex $argv 1], but when i call Port script from this script it doesnot run correctly and error tell me correct line
"/home/metro $3 $1 $4 $5 $6 "


Posted by admin (Graham Ellis), 28 February 2006
Oh - I think I spot your problem.   If you run expect of Tcl from an interactive shell, you can also run shell commands directly in there. However, once your run it as a script you'relimited to Tcl commands.   But you've put your port command directly into the script which you can't do - it's not itself an expect command.  I guess you wantg to send it - thus

send /home/mansoor/Port $3 $1 $4 $5 $6

Posted by mansoorx (mansoorx), 28 February 2006
switch#can't read "3": no such variable
   while executing
"send /home/mansoor/Port $3 $2 $4 $5 $6"
   (file "/home/mansoor/sw" line 12)

Posted by admin (Graham Ellis), 28 February 2006
My error - use the lindex system just like you're now using for all the other command line parameter uses.

Posted by mansoorx (mansoorx), 1 March 2006
I am using this script to take the switch port configuration, currently i store different commands result in different files i want to store these three result in a single file, what i do ?  

Posted by admin (Graham Ellis), 1 March 2006
Hi, Mansoor.  I feel in great danger of writing a course on programming principles via this forum - and that's not the best way for you to learn (it would be slow and frustrating) nor the bext use of my time.  You would do far better using a book (list of 20 books on Tcl/Tk and Expect)  or attending an appropriate course (details) and using the resource of this forum on occasions when you come across a sticky problem that's not fixed by the books and course knowldege gained.

The answer to your immediate question.   If you want to write seveal things all to the same file, open it once and use multiple puts statements.  Each puts statement will add more in trun onto the end of the same file if you use the same channel variable.

This page is a thread posted to the opentalk forum at 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., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho