Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/2474_Usi ... -jobs.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
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))
Using Tcl and Expect to automate repetitive jobs

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)


Back to
Exploring Old Railways
Previous and next
or
Horse's mouth home
Forward to
Quick easy and dangerous - automated logins via Tcl / Expect
Some other Articles
How did I do THAT?
By train ...
Tcl - uplevel to run code at calling level
Quick easy and dangerous - automated logins via Tcl / Expect
Using Tcl and Expect to automate repetitive jobs
Exploring Old Railways
split and join in tcl and expect
A short form of if ... then ... else
Windows 7 and Open Source Programming
Beyond the Pale
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

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

PAGE: http://www.wellho.net/mouth/2474_Usi ... -jobs.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb