Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Expect - choreographing multiple pings and analysing result
Interprocess Communication example from a Well House Consultants training course
More on Interprocess Communication [link]

This example is described in the following article(s):
   • Expect in Perl - a short explanation and a practical example - [link]

Source code: check Module: P223
#!/usr/bin/perl

use Expect;

# Subroutine to handle each line returned

sub linebyline {
        # Grab the information from the expect_out buffer
        $line = $exp->before();
        if ($line =~ /\((\d+\.\d+\.\d+\.\d+)\)/) {
                print "At $1\n";
                }
        if ($line =~ /time=([\d\.]+)\s*(\w+)/) {
                print "Round trip - $1 $2\n";
                }
        if ($line =~ /(\d+) packets transmitted, (\d+) packets received/) {
                print "$2 of $1 received\n";
                $result{$host} = ("Failed","1/3","2/3","AOK")[$2];
                }
        }

@hosts = ("www.wellhousemanor.co.uk","www.wellho.net","www.sheepbingo.co.uk",
        "control.wellho.net","office.wellho.net","manor.wellho.net",
        "wellho.net");

# Loop through each host of interets

foreach $host(@hosts) {

print "\nChecking $host ...\n";
$result{$host} = "no result recorded";

# start a spawn process
$exp = Expect->spawn("ping -c3 $host") or die ("Cannot Spawn");
# turn off echo of results on to screen
$exp->log_user(0);

$trigger = 1;

# read results backline by line, with timeout and end of file trapped
while ($trigger) {
        $exp->expect( 5,
                [qr"\r\n" => \&linebyline ] ,
                [timeout => sub {$trigger = 0; print "TIMED OUT\n";
                        $result{$host} = "Timed Out"; }] ,
                [eof => sub {$trigger = 0}] );
        }

}

# Print final analysis table

print "\nCompleted ... \n";
foreach $host(sort {$result{$a} cmp $result{$b}} keys %result) {
        printf ("%-10s %s\n",$result{$host},$host);
        }

__END__

Sample output:

wizard:csr2 graham$ ./check

Checking www.wellhousemanor.co.uk ...
At 217.160.182.81
Round trip - 100.171 ms
Round trip - 68.998 ms
Round trip - 84.314 ms
3 of 3 received

Checking www.wellho.net ...
At 83.170.95.163
Round trip - 29.984 ms
Round trip - 50.758 ms
Round trip - 130.806 ms
3 of 3 received

Checking www.sheepbingo.co.uk ...
At 82.165.98.111
Round trip - 43.693 ms
Round trip - 44.986 ms
Round trip - 48.093 ms
3 of 3 received

Checking control.wellho.net ...
TIMED OUT

Checking office.wellho.net ...
At 82.46.84.213
Round trip - 110.232 ms
Round trip - 111.055 ms
Round trip - 130.407 ms
3 of 3 received

Checking manor.wellho.net ...
At 94.169.96.118
Round trip - 173.806 ms
1 of 3 received

Checking wellho.net ...
At 64.62.240.12
Round trip - 475.397 ms
Round trip - 398.913 ms
Round trip - 316.539 ms
3 of 3 received

Completed ...
1/3 manor.wellho.net
AOK wellho.net
AOK office.wellho.net
AOK www.wellhousemanor.co.uk
AOK www.wellho.net
AOK www.sheepbingo.co.uk
Timed Out control.wellho.net
wizard:csr2 graham$

Learn about this subject
This module and example are covered on our public Perl Extra course. If you have a group of three or more trainees who need to learn the subject, we can also arrange a private or on site course for you.

Books covering this topic
Yes. We have over 700 books in our library. Books covering Perl 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 "Interprocess Communication" training module. You'll find a description of the topic and some other closely related examples on the "Interprocess Communication" 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 or under an Open Training Notes License from our sister site http://www.training-notes.co.uk.

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.

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

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.

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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sat Apr 13 08:12:34 2013 • BUILD SYSTEM: wizard