If you're typing the same series of instructions into your computer time and time again, perhaps with predictable variations depending on the responses that you get, you should consider using
Expect. Expect adds three extra major commands to the
Tcl language:
•
spawn to start a process
•
send to send a (user input) to that process
•
expect to tell the program what to expect back.
There's a huge power in the
expect command - you can have it wait for an exact string, a pattern (matched with directory style 'globbing' or regular expressions), or the first of several matches if you like. You can even spawn multiple processes and wait for any of them to respond with a match, and you can set up various fallbacks using
expect_before and
expect_after commands which let you trap any error messages that you get back in a common format without having to specify them all through the question and answer sequence.
However ... that's getting a bit advanced for an introduction. So let's have a look at a simple piece of code, as written on last week's
Tcl Programming Course:
puts "This is a Tcl program"
OK - I started with that just to prove that this really IS Tcl running ;-). Then the spawn - expect - send sequence:
log_user 0
spawn ssh -l username www.wellho.net
expect "sword: "
send "xyzyxzxxw\r"
expect "3.2\$ "
send "tail -60 /home/wellho/logdir/filename.txt\r"
expect "3.2\$ "
set store $expect_out(buffer)
send "exit\r"
Some notes on that:
1. You'll run the code at first WITHOUT the
log_user command; on your initial run, you'll want to see the interaction as it happens, so that you can debug it / fix any places where it hangs or times out.
2. You don't need to describe the full string you're expecting - just the end of it. The word "password" ends with "sword". However, you MUST add the ": " on the end of it if that is how the prompt ends so that the password isn't sent too early!
3. The \r means "carriage return" which is the key you press on your keyboard to send data. You do NOT send a new line (\n) when you press enter!
4. The
expect_out array contains a great deal of interesting data after you've run an expect command, including the text that's been received since your last expect. Save it, use it!
As you get data back in you expect command, via the expect_out array, you can analyze it in Tcl. In the example above, where we saved the total response into a variable called
store, we concluded the program with the following analysis:
set counter ""
foreach line [split $store \n] {
set lastmin [lindex $line [expr [llength $line] -3]]
regsub "," $lastmin "" load
lappend counter $load }
puts $counter
And the result - because the log file that we read was a server log noting how busy our system was - is a list of loads every minute for the last hour:
0.21 0.18 0.19 0.27 0.30 0.23 0.12 0.62 0.44 0.17 0.36 0.13
0.17 0.13 0.19 0.29 0.31 0.42 0.58 0.47 0.30 0.22 0.30 0.45
0.33 0.58 0.71 0.33 0.25 0.48 0.22 0.40
The complete source of this example is available
here (written 2009-10-24)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T212 - Tcl/Tk - Expect Processes [287] Checking that all our servers are up and accessible - (2005-04-22)
[675] Adding PHP tags to an old cgi program - (2006-04-08)
[1173] Cheat Sheet / Check list for Expect maintainers - (2007-05-02)
[1785] What is running on your network? (tcl and expect) - (2008-09-04)
[2489] Parallel Pinging, using Python Threads or Expect spawn lists - (2009-11-02)
[3448] Checking all the systems on a subnet, using Expect and Tk - (2011-09-18)
[4678] Expect with Ruby - a training example to get you started - (2016-05-18)
T211 - Tcl/Tk - What is Expect? Why use it? [286] Automating regular manual procedures - (2005-04-21)
[435] Expect for Windows - (2005-09-04)
[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)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
[3286] Should we cover expect and/or Tk on our public Tcl courses? - (2011-05-11)
[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)
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
How did I do THAT?By train ...Tcl - uplevel to run code at calling levelQuick easy and dangerous - automated logins via Tcl / ExpectUsing Tcl and Expect to automate repetitive jobsExploring Old Railwayssplit and join in tcl and expectA short form of if ... then ... elseWindows 7 and Open Source ProgrammingBeyond the Pale