Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
 
Web based script - ping a series of remote hosts
Expect Processes example from a Well House Consultants training course
More on Expect Processes [link]

This example is described in the following article(s):
   • Checking that all our servers are up and accessible - [link]
   • Adding PHP tags to an old cgi program - [link]
   • Hot answers in PHP - [link]

Source code: livetest.exp Module: T212
#!/usr/bin/env expect

# This was originally written as a sample script to show
# how expect can be used on a web server to monitor other
# systems. We've extended it as a live script on our
# own web sites - this is the version that is live at the
# time of writing

set hosts {www.wellho.net
    www.wellho.co.uk
    www.sheepbingo.co.uk
    office.wellho.net
    www.melkshamhotel.co.uk
    www.savethetrain.org.uk
    www.wellhousemanor.co.uk
    www.wellho.info
    www.training-notes.co.uk}

set clients {www.avairpros.com
    www.express-cleaning-supplies.co.uk
    www.gregpullen.co.uk}

set localhosts {hugo fire coffee dolphin pepsi
    saturn mars}

set trainees {tormentil tansy thyme parsley nutmeg flipper
    chopsuey bhajee vindaloo snowdrop
    buttercup elderberry daisy dandelion pasanda foxglove
    }

# If running on our Intranet, test local and client systems instead

if {[regexp {192\.168\.200|127\.0\.0} $env(SERVER_ADDR)]} {
    set hosts [concat $clients $localhosts $trainees]
    }

log_user 0
set timeout 25
set runningcount 0

# Parallel ping all the systems we're testing

foreach system $hosts {
    spawn ping -c 2 $system
    lappend idlist $spawn_id
    set realhost($spawn_id) $system
    incr runningcount
    }

# 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 {
        break
        }
    }

}

puts "content-type: text/html\n"
set goodstuff ""
set badstuff ""
set good 0
set bad 0

foreach which [array names ans] {
    set posthost 0
    set host "NULL"
    set count 0
    foreach line $ans($which) {
        if {$posthost == 1} {
            set count "undef"
            regexp "\(\[0-9\]\) packets received" $line all count
            # puts "$host - returned $count packets"
            set posthost 2
            }
        if {[regexp ^---\ \(\[^\ \]+\) $line all host]} {
            set posthost 1
            }
        }
    if {$count == 2} {
    append goodstuff "<a href=http://$realhost($which)>"
    append goodstuff "$realhost($which)</a> is available<br>"
    incr good
    } else {
    append badstuff "$realhost($which) is NOT available<br>"
    incr bad
    }
}

set stuff {<span class=subhead><b>System report -
Well House Consultants</b></span></br><br>
<span class=body>Report generated }
append stuff [clock format [clock seconds]]
append stuff {<br><br>}
append stuff "<!-- start -->"
append stuff "<i>Operational ...</i><br>$goodstuff<br><br>"
append stuff "Total of $good systems<hr>"
append stuff "<i><b>Not operational ...</b></i><br>"
append stuff "$badstuff<br><br>Total of $bad systems"
append stuff "<br><!-- end -->"
append stuff {
<br><br>This page is generated by a script written in Expect
(an extension of the Tcl language) and was written by Well
House Consultants who provide <a href=/course/tcl-tk.html>
training on Tcl, Expect and Tk</a>. To learn a
little more about this script, please visit the associated
<a href=/resources/pinginfo.html>information page</a><br>
}

set fh [open template/page.htp r]
set content [read $fh]

regsub %ginc% $content "" content
regsub %cookie% $content "" content
regsub %seepic% $content "" content
regsub %finish% $content "" content
regsub -all %lhs% $content "ctco" content
regsub %topimg% $content "WHhead2.gif" content

regsub %stuff% $content $stuff content
regsub -all %title% $content "Well House System Status" content

regsub -all {<\?php[^\n]*locate[^\n]*?\?>} $content \
        "international site" content
regsub -all {<\?php[^\n]*voiceline[^\n]*?\?>} $content \
        "+44 (0) 1225 708225" content
regsub -all {<\?php[^\n]*today[^\n]*?\?>} $content \
        [clock format [clock seconds]] content
regsub -all {<\?php[^\n]*faxline[^\n]*?\?>} $content \
        "+44 (0) 1225 707126" content
regsub -all {<\?php[^\n]*copyyear[^\n]*?\?>} $content \
        "2005" content
regsub -all {<\?php.*?\?>} $content "" content

puts $content

Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in Tcl
 * Tcl Programming
 * Tcl Programming
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Tcl, Tk and Expect are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Expect Processes" training module. You'll find a description of the topic and some other closely related examples on the "Expect Processes" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Jan 31 13:04:01 2016 • BUILD SYSTEM: WomanWithCat