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.
Web Front - TCL/Expect Telnet Backend

Posted by jkurrle (jkurrle), 7 October 2004
As many posters here, I am a newcomer to the TCL/Expect environment.  However, despite my lack of previous training, I have found the language to be easily adaptable, except for one issue.

I have an Expect script that spawns a Telnet session and works flawlessly.  It's great when I run it from a command line.  I also have figured out how to pass parameters from a web form into a TCL CGI script.  What I am running into is an issue with running a background automated telnet session on a web server.

This is procedurally what I want to do:

1) User selects some variables on a web page form and presses the form button to submit.  (Got this)
2) Web form sends data to a TCL CGI script (this works)
3) The TCL/Expect script then spawns an automated background Telnet session, using the supplied parameters to manipulate a proprietary piece of hardware.

The problem I have is that once I try to spawn the Telnet session, the script errors out.  Does someone have a working example of a web spawned background telnet session that I can take a look at and see what I'm doing wrong?

Any help would be appreciated.  Thank you.


Posted by admin (Graham Ellis), 8 October 2004
As a quick guess, I suspect you aren't doing a
    log_user 0
and you're spawning your telnet before you send out your script headers?

Here's a working examle of expect used on a web page:


puts "Content-type: text/html\n"
puts "<html><head></head><body><h1>My Example</h1>"

log_user 0
spawn uname -n
expect "\$\ "

puts "Server real name is <b>$expect_out(buffer)</b><br>"
puts "<br>Copyright <a href=>Well House Consultants</a> 2004";

puts "</html>"

And you can run it via

Please post up a short piece of sample code to show the problem, and be a little more specific about how it fails (exact error message and/or description).  That will help me give further advise.

Posted by neo (The Architect), 22 January 2005
Slightly off topic but thought i might ask here anyway.
Can we automate FTP sessions using Tcl. I mean how do we send the password after connection to port 21 on any machine?

BTW is it necessary that sockets are used? is there any way i could just send out the 'ftp' commands from the Tk script and wait for a response?

Posted by admin (Graham Ellis), 22 January 2005
Your side text says "the problem is choice" and how right you are.

Yes - you could simply use sockets (but as I understand FTP, it uses both a TCP and UDP connection ans isn't the easiest) OR you could use a prewritten set of procs that someone else has done the hard work with OR you could use expect. Personallu, I would use Expect provided that I had a suitable Unix / Linux based system (although I do see that ActiveState now offer a commerciaol license for a Windows expect that's probably good).

There's a simple example of using FTP through expect on our tcl course - here's a direct link

Posted by neo (The Architect), 22 January 2005
I would like to use Tk for a GUI interface. In this case then if I use expect will not my app be dependent on both TCL, Tk and also expect (if i use expect)?
What if any one of those (tk OR expect)is not available?

Posted by neo (The Architect), 23 January 2005
If TK is present can i assume that expect will also be present?

Posted by admin (Graham Ellis), 23 January 2005
Both Tk and Expect require a "core" Tcl installation - so if you have expect or Tk you automatically have Tcl.

Tk is a GUI, so it needs a system with a good graphics basis underlying; X windows or a Microsoft window system is fine.

Expect makes a number of operating system calls and works in such a way that it needs a good underlying multiuser operating system on which to fucntion well.  Historically, that's meant a Unix varient as the microsoft products originated from the desktop rather than from the server, and Unix systems the opposite way.  However, these days most (if not all) of the weaknesses have been overcome in both directions.

Expect has been a very stable piece of software for many years - quite simply, it's fulfilled its function well, so why change it?  The result, though, has been that support onto recent versions of Windows has been poor.  The ActiveState version of Expect (for which you need a license ....) should be plugging the gap.

Posted by neo (The Architect), 23 January 2005
How do i use expect scripts from my Tk app?
I tried the expect commands but they gave me errors.

Posted by admin (Graham Ellis), 23 January 2005
You probably use a different executable with a name like expectk into which both are built

Posted by neo (The Architect), 24 January 2005
is that expectk available for Linux/ Cygwin?

Posted by admin (Graham Ellis), 24 January 2005
Linux - yes, for sure. Use it all the time.
Cygwin - dunno ... try googling for it  

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