Expect scripting tutorial

Expect is a useful tool for feeding inputs automatically to an interactive program. Had used it recently to test my code and came away impressed. Here`s a short, basic tutorial to get u started

Installation:
1) You need 2 things – the tar file of expect and the tar file for tcl. (expect needs tcl)
Just google for the these 2. You`ll find them easily. They are normally in .tar.gz format. Download them .
http://www.tcl.tk/software/tcltk/download.html (tcl)
http://expect.nist.gov/#unix (expect)
2) tar -xvf tcl.tar.gz
3) go into the tcl folder and the unix folder inside that
4) Execute : ./configure –prefix=/home/netradio/tcl
5) Untar the expect file too.
6) Go into the expect folder and type : ./configure –with-tcl=/home/netradio/Expect/tcl8.4.19/unix
Expect will need the tclConfig.sh file in that path
7) Then say “make” and lo ! you have the ‘expect’ binary

Tutorials :

http://users.csc.calpoly.edu/~dbutler/tutorials/winter96/expect/tutorial.html

http://www.tcl.tk/man/expect5.31/expect.1.html

http://expect.nist.gov/FAQ.html

Then go to your directory and make a softlink of expect there :
ln -s <path of expect> expect

The three commands send, expect, and spawn are the building power of Expect. The send command sends strings to a process, the expect command waits for strings from a process, and the spawn command starts a process.

For a start execute expect. IT will show you a prompt like :
expect1.1>

Here give the command: send “hello world”

You can also store commands in a file and execute them with expect.
Eg – enter send “hello world\n” in a file
And execute, ./expect <file-name> and the script will execute the statements

This is sthg even kewler :
You can create a file with commands.
#!./expect -f
should be the first line
Type your commands below
The file could look like :
—————————-
#!./expect -f
send “hello world\n”
———————————–
Then, make the file an executable by :
chmod +x <fileName>
Then just say ./<fileName>…
and chill out :-)

Now make a change to your file :
—————————-
#!./expect -f
expect “hi”
send “hello world\n”
———————————–
When you execute this, the script will wait for the string “hi” to be entered. When this string is received .. it will immediately print “hello world” and exit. If u enter anything other than “hi”, it will continue waiting

If expect reads characters that do not match the expected string, it continues waiting for more characters. If I had type hello instead of hi followed by a return, expect would continue to wait for “hi\n”. Finding unexpected data in the input does not bother expect. It keeps looking until it finds something that matches. If no input is given, expect command eventually times out and returns. By default, after 10 seconds expect gives up waiting for input that matches the pattern. This default value can be changed by setting the variable timeout using the Tcl set command. For example, the following command sets the timeout to 60 seconds.
set timeout 60
A timeout of -1 signifies that expect should wait forever and a timeout of 0 indicates that expect should not wait at all.

(you need to set the timeout before first expect command)

Anchoring
To prevent expect from matching unexpected data, expect patterns can include regular expressions. The caret ^ is a special character that only matches the beginning of the input; it cannot skip over characters to find a valid match. For example, the pather ^hi matches if I enter “hiccup” but not if I enter “sushi” . The dollar sign ($) is another special character. It matches the end of the data. The pattern hi$ matches if I enter “sushi” but not if I enter “hiccup”. And the pattern ^hi$ matches neither “sushi” nor “hiccup”. It matches “hi” and nothing else.
Patterns that use ^ or $ are said to be anchored. When patterns are not anchored, patterns match beginning at the earliest possible position in the string.

Pattern-Action Pairs
Expect also allows association between a command and a pattern. The association is made by listing the action (also known as command) immediately after the pattern in the expect command itself. Here is an example of pattern-action pairs:
expect “hi”  { send “You said hi\n” } \
“hello”   { send “Hello yourself\n” } \
“bye”     { send “Good-bye cruel world\n” }

This command looks for “hi”, “hello”, and “bye”. If any of the three
patterns are found, the action immediately following it gets executed.
If there is no match and the default timeout expires, expect stops
waiting and execution continues with the next command in the script.

The spawn Command
The spawn command starts another program. The first argument of the spawn command is the name of a program to start. The remaining arguments are passed to the program. For example:
spawn ftp ftp.uu.net
This command spawns an ftp process and ftp.uu.net is the argument to the ftp process.

Putting It All Together
Small examples i tried out  :
//————————————————————————–
c++ code :
int i = 0;
printf(“enter:”);
scanf(“%d”, &i);
printf(“You entered %d\n”, i);
//———————————————————————————–
expect code :
#!./expect -f
spawn /home/netradio/myprogs/a.out
expect “enter:”
send “25\r”
interact
//———————————————————————————–
//———————————————————————————–

worked great !
The “interact” statement is very important. Doesn`t work without that. It is needed once at the end of your script

10 comments on “Expect scripting tutorial

    • Thanks a lot for your generous comments, Sleek… ! :-) Actually am not too sure about the mechanics of “interact” comment myself. Had to learn expect scripting for a small task, so learnt just enough to do the job… didn`t explore it too much after that. The tuty is pretty basic.. glad it helped. Hope you`ll master the scripting lang and come out with a better tuty for all of us :-)

  1. I think the “interact” line keeps whatever you spawned running and hands the control over to standard input again after the script quits. I tried without it, and it terminates the process it spawned as it terminates itself.

  2. Pingback: Automatic scp command « toto-share.com

  3. In case, if you have written the code without interact command, then the script would exit immediately after sending the string “25\r”. Interact command does the control, hands over the job to the addition process, and produces the expected result.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s