Expect-Lite

Making Expect scripting Simple

Installation and Quick Start Guide



Quick Install

Place expect-lite in your path. Test by typing at the prompt:
expect-lite
If you see the help, then it is ready to go, installation is complete!

Quick Start

Expect-lite has many features which make it useful, and the documenation highlights those. But to start using expect-lite quickly, you require two items:
  1. a script
  2. a host 
Here is a quick script which you can paste into a file to test expect-lite, call it test.elt :
; ==== Ping localhost and report result ====
@5
>ping -c 2 localhost
+$packet_rx=([0-9]+) received
>
>echo "Packets received are:$packet_rx"
The second item you require is a host to run your script against. Expect-lite is designed to log into a remote host and run your script, as if a person was typing the commands. However that may require setting up ssh keys and so forth, and this is the Quick Start section. Type the following to run the above script on your localhost:
expect-lite r=none c=test.elt
There should be output that looks similar to the following:
cvmiller@maile:~/Freescale$ 
cvmiller@maile:~/Freescale$ ping -c 2 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.056 ms

--- localhost ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.053/0.054/0.056/0.007 ms
cvmiller@maile:~/Freescale$
Assigned Var:packet_rx=2


cvmiller@maile:~/Freescale$ echo "Packets received:2"
Packets received:2

##Overall Result: PASS
Congratulations you have just run your first expect-lite script! To find out more details on tuning see More Details.

BSD-Style License

Copyright (C) Freescale Semiconductor, Inc. 2005-2007

See the  COPYING for information on usage and redistribution
of this file, and for a DISCLAIMER OF ALL WARRANTIES.

Download

Expect-lite downloads can be found on the Expect-lite Project Page: http://sourceforge.net/projects/expect-lite
The package includes expect-lite, as well as several examples.

More Details

Expect-lite was created to prevent the scriptor, you, from having to write expect scripts in TCL. However with that in mind, there are a few TCL details one should know that will tune expect-lite to perform better in your environment.

Since expect-lite is written in TCL, an interpretive language, making changes to the global variables controlling the behaviour does not require recompilation.

There are several global variables inside expect-lite which will help tune it to your environment. Open your favourite editor, and load expect-lite. The following sections will cover the tuneable variables:

Default Timeout

The nature of expect, is that something is sent, and something else is expected to return, expect-lite will wait until the timeout value for that something to return. If the required string is not returned in the defined time, expect-lite will declare the test failed, and return non zero.

set timeout 30 

The default timeout is used to set a reasonable value for a timeout when the user script has not set a timeout. Once the user script has set a timeout (eg. @60), the default timeout will no longer be used.

Infinite loop protection

With the addition of looping support in expect-lite (version 3.0.0+), infinite loop protection has been implemented to prevent user scripts from looping forever.
set _el_infinite_loop 500
Expect-lite detects an infinite loop by decrementing this value each time the script loops. Once this value is zero, expect-lite will print and Error, and fail the test. It is important to know that expect-lite does not detect multiple sequencial loops in the user script, and therefore the value of _el_infinite_loop should be able to accommodate all desired looping in the user script. Typically this will be between 100 and 1000 depending on how much looping is performed in the user scripts.

User Name and Password

Expect-lite supports three connection methods:
  1. telnet
  2. ssh
  3. ssh_key
The user name and password will be used for connection methods 1 & 2. Clearly method 3 is more secure since the user name and passwords are not placed in expect-lite. However this may not be practical for your environment.
set user "root"
set pass "123456"
If the user name is not blank (eg. set user "") access method 3 will use that user name rather than the one of the current login when exchanging ssh_keys.

Connect Method

As mentioned above, expect-lite supports three methods to connect to remote hosts. The ssh_key method is the most secure, as no passwords are utilized. However this requires setting up ssh keys prior to using this method.
# connects to login host, and logs in as $USER
#
# Choose one login method here
#
#set connect_method "telnet"
#set connect_method "ssh"
set connect_method "ssh_key"
There is a forth method, as shown in the Quick Start, where no remote login is performed. This method is primarily for the Quick Start section, and has not been extensively tested. To invoke this method the value of "none" is given for the remote host on the command line.
expect-lite remote_host=none cmd_file=test.elt

Print Warnings

Expect-lite will always print Errors, and should fail the test imediately, should it encounter and error (eg. loss of connection to remote host). However additional information has been made available under warnings. Examples of what is printed under warnings are:
set WARN 1
If this additional information is not required, setting WARN to 0 will turn off this additional chatter.

Remote Shell

Expect-lite has been tested, and designed to get along with bash. However others may want to use another shell.
set remote_shell "bash"
Changing this value will change the default shell upon login to the remote host. The script writer can over ride this value by issuing a command in the user script such as:
>csh
A cshell will be invoked and all subsequent shell operations will be utilizing the cshell.

Delay to wait for remote host

Because of the nature of accessing a remote host, expect-lite may have to wait for a response if the access is slow (eg. over a VPN via the internet).
# Delay (in ms) to wait for host in Not Expect, and Dynamic Var Capture
set delay_wait_for_host 100
The value of 100 ms is a good value for a local LAN, while 200 ms may be desired if running across high speed internet. As this value is increased the script will run slower. It is recommended that a minimum value be used to prevent scripts from taking too long to run.

Wait for Prompt

What is a prompt? Does it include colour? Does it end in >, $, # ?

There is an implied "wait for prompt" everytime the script writer uses the send command (eg. >ls ) It helps by not issuing the next command before the previous command has completed.

A generalized procedure has been supplied which should detect most prompts.
proc wait_for_prompt {} {
global timeout WARN
#Accept '>' '#' '%' or '$' prompt
if { $timeout == 0 } {
# delay in ms
after 50
}
#Added the additional ".{0,9}" to prompt incase there are colours in the prompt
expect {
-re ".*> .{0,9}$" { }
-re ".*# .{0,9}$" { }
-re ".*% .{0,9}$" { }
-re {.*\$ .{0,9}$} { }
timeout {
if {$WARN} { puts "Warning:Prompt Timed Out!" }
}
}
}
To modifiy this requires some knowledge of TCL. If there is a prompt that is not supported by this procedure, this is where it should be added.



COPYING


Copyright (c) 2007, Freescale Semiconductor, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the Freescale Semiconductor nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



4 November 2007
http://expect-lite.sourceforge.net/

this document for version 3.0.3