A sizeable minority of our Tcl course delegates use the
expect and / or
tk extensions. And on our Tcl courses we handle the two differently. For Expect, we include some coverage in our standard agenda, running if required an hour or two into the evening of starting early in the morning to ensure substantial coverage if it's required. For the Tk toolkit, we also include a brief demonstration on the main course, but will then add an extra day or two - a whole extra course - for people who need it. That's because Tk includes a very large number of extra commands and facilities, whereas expect adds a great deal in a few extra commands.
This week, expect is something of a side issue - so a brief demo at the end of yesterfay afternoon was all that was needed. I used the example of wishing to check a series of hosts (sequentially) with ping, and report back on the round-trip time to each. Full source code at
[here]
expect is Tcl ... with extra commands, so a
foreach loop through a series of hosts is the natural way to test each host in turn without repeating code:
foreach mysite {www.wellho.net wellho.net www.wellho.co.uk www.firstgreatwestern.info} {
Within each itereation, we start off a
ping progess through the spawn command:
spawn ping -c 3 $mysite
and wait for reply text to include the words "round-trip" which comes late in the response:
expect "round-trip"
We then use a
regexp to extract the vital data we want to report froma everything that expect has dumped into a buffer thats avaulable to us, and report it withing a
puts command:
regexp {time=([0-9]+\.[0-9]+)} $expect_out(buffer) all trip
puts "$mysite came back with a trip time of $trip"
Result?
munchkin:tclmay11 grahamellis$ expect xpx
www.wellho.net came back with a trip time of 21.070
wellho.net came back with a trip time of 164.911
www.wellho.co.uk came back with a trip time of 18.176
www.firstgreatwestern.info came back with a trip time of 30.522
munchkin:tclmay11 grahamellis$
As always, the devil is in the detail; my program will fail if any of the hosts isn't reachable doesn't respond, and I could add code to deal with that - the facilities are there. However, that would muddy the first training example, and in any case if the program's run and fails that's an indication in its own right that you have a problem in this case.
Tcl (and expect) course details -
[here].
(written 2011-05-11)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T211 - Tcl/Tk - What is Expect? Why use it? [286] Automating regular manual procedures - (2005-04-21)
[435] Expect for Windows - (2005-09-04)
[1173] Cheat Sheet / Check list for Expect maintainers - (2007-05-02)
[1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
[1409] What is Expect? - (2007-10-26)
[1411] Buffering of inputs to expect, and match order - (2007-10-27)
[1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1531] Expecting a item from a list of possibles - (2008-02-04)
[1602] Automating processes through Expect - (2008-04-05)
[2474] Using Tcl and Expect to automate repetitive jobs - (2009-10-24)
[2489] Parallel Pinging, using Python Threads or Expect spawn lists - (2009-11-02)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
[3572] Adding Expect on top of Tcl - what is it and where can I get a training course to learn about it? - (2012-01-08)
[4405] Backup procedures - via backup server - (2015-01-24)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
T050 - Tcl/Tk - Tcl, Tcl/Tk and Expect - General [2429] Tcl scripts / processes on a web server via CGI - (2009-09-27)
[2504] Learning to program in ... - (2009-11-15)
[2681] Tcl - a great engineering language - (2010-03-17)
[3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
[4206] Writing the perfect program in Tcl? - (2013-11-13)
[4434] Public training courses - upcoming dates - (2015-02-21)
[4460] Using Object Oriented Tcl and the Tk toolkit together - real life example - (2015-03-12)
[4616] Still teaching Tcl in 2016? - (2016-01-06)
Some other Articles
Changes (and no changes) at Melksham bus stopsNew Camera - very first picture, and next from first filmsRandom Questions ...Exceptions - Tcl styleShould we cover expect and/or Tk on our public Tcl courses?Extracting data from a string / line from file - TclWhat to do in the evening during a Tcl courseThe jugglerThe future of canal management and charities - Kennet and Avon Canal biasDoes Well House Manor Hotel in Melksham offer lots of discounts?