0% found this document useful (0 votes)
121 views

Expect Basics: Tutorial 3

This document provides an overview of the Expect command in 3 tutorials. It discusses the basic commands of Expect including send, expect, and spawn. It shows how to interact with other processes, handle errors and timeouts. It also covers glob pattern matching and regular expressions in Expect.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Expect Basics: Tutorial 3

This document provides an overview of the Expect command in 3 tutorials. It discusses the basic commands of Expect including send, expect, and spawn. It shows how to interact with other processes, handle errors and timeouts. It also covers glob pattern matching and regular expressions in Expect.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Expect Basics

Tutorial 3

ECE453 & CS447

Software Testing, Quality Assurance and Maintenance


Tutorial Overview

1. Central commands of Expect


2. Interact with other processes
3. Error and timeout handling
4. Glob Pattern Matching in Expect
5. Regular Expression pattern matching in Expect

ECE453 & CS447 Tutorial-3 Expect Basics 2


Getting Started with Expect
• Executing Expect
$ expect
– Using Expect interpreter expect1.1> exit
$
– Executing Expect script
$ expect script-filename
files from Unix. ……
– For simplicity, > is used as prompt for Expect interpreter.
• Three central commands: send, expect and spawn.
– send: send a string to a > send “Hello world”
connected process. Hello world>

– It does not format string $ expect speak


in any way. Hello world$
– Can save the command to a file and execute it from Unix
shell directly. (assume speak is that file)

ECE453 & CS447 Tutorial-3 Expect Basics 3


Command: expect
expect command waits for a string > expect “hi”
from a process, the default is from philosophic //input from keyboard
keyboard. >
Syntax: > expect “hi” {
expect [ pattern-string {action} ] send “hello there\n”
}
It waits for first occurrence of philosophic //input from keyboard
pattern and stop only when match hello there //command output
is found or timeout. >
The match pattern string is stored > expect “hi” {
in expect_out(0, string) and the send $expect_out(0, string)\n
entire string read is kept in send $expect_out(buffer)\n
expect_out(buffer). }
philosophical //input from keyboard
The remain string “cal<newline>”
hi //value of expect_out(0,string)
is still in queue and available for
phi //value of expect_out(buffer)
next expect command.
>
ECE453 & CS447 Tutorial-3 Expect Basics 4
Pattern-Action Pair for expect
expect can search > expect “hi” { send “hello there\n”
several patterns } “hello” { send “hello yourself\n”
simultaneously (like } “bye” { send “this is unexpected\n” }
switch-case) }
tmklbyetasd //input from keyboard
this is unexpected //command output
>
Another format to > expect { “hi” { send “hello there\n”}
handle long line “hello” { send “hello yourself\n”}
command. “bye” { send “this is unexpected\n”}
}
>
> expect “hi” { send “hello there\n”} “hello” { send “hello yourself\n”}
> expect “hi” send “hello there\n”

Incorrect command format Correct command format

ECE453 & CS447 Tutorial-3 Expect Basics 5


Command: spawn
spawn command starts a process $ ftp ftp.uu.net
and treats user as process too. Connected to ftp.uu.net
220 crete FTP server (SunOS 5.6) ready.
Name (ftp.uu.net:jlian): anonymous
Syntax: 331 Guest login ok, email addr as passwd.
spawn program-name argument-list Password:
230 Guest login ok, access restricted.
Example for create a ftp ftp> quit
connection to a server in Unix shell $

spawn ftp ftp.uu.net


Example by using expect to do expect “Name”
same thing automatically. send “anonymous\r”
expect “Password:”
These commands needs to be saved send “[email protected]\r”
in a script file. expect “ftp> ”
send “quit\r” //quit from ftp

To simulate user press return key, use \r, don’t use \n.

ECE453 & CS447 Tutorial-3 Expect Basics 6


ftp Example
This example uses a script file to
# copy file from specified directory
establish a connection to specified # in a specified server by ftp
server (ftp.uu.net) and copy a file
from specified directory. if { [llength $argv] < 2} {
This file is saved in a file named puts "usage: expect run dir file"
run. It is executed from Unix shell exit 1
directly by using expect. }
set timeout -1
spawn ftp ftp.uu.net
$ expect run tmp milk.c expect "Name"
spawn ftp ftp.uu.net send "anonymous\r"
Connected to ftp.uu.net.
… expect "Password:"
230 User logged in. send "[email protected]\r"
ftp> cd tmp expect "ftp> "
250 CWD command successful. send "cd [lindex $argv 0]\r"
ftp> get test
150 Opening … for milk.c (1149 bytes). expect "ftp> "
226 Transfer complete. send "get [lindex $argv 1]\r"
local: milk.c remote: milk.c expect "ftp> "
1200 bytes received in 0.16 seconds … send "quit\r"
$

ECE453 & CS447 Tutorial-3 Expect Basics 7


Command: interact
interact command gets back control set timeout 30
from expect to user. After … //establish connection
execution, expect stops reading expect timeout {
command from script. The user interact
input from keyboard will be directly puts "unexpected…"
sent to spawned process and its } "ftp> " {
output is sent to standard output . send "do some ftp commands\n"
} "^\[45]" { //this is common error #
When error occurs during interact
execution ftp commands, the user puts “Error … "
gets control back. }

When no matching found or no input available, expect waits for a period specified
by timeout variable, (default 10s). It should be an integer.
We can use timeout as a special pattern in expect (no double quota). Example
shown above. It catches timeout signal and performs related actions.

ECE453 & CS447 Tutorial-3 Expect Basics 8


Glob Patterns of Expect
• Three glob pattern *, ? and [ ] inherited from Tcl
– *: match the longest possible string
– ?: match any single character.
– [ ]: match any single character specified in [ ].
• Examples
– a?b: match a2b, but not adcb.
– [abcdef0123456789]: match any hexadecimal digit
– [a-f0-9]: same as above > expect “[a-f]” { …}
• Special treatment for [] invalid command name "a-f"
>
– Example doesn’t work
> expect “\[a-f]” { …}
– Two solutions
>expect {[a-f]} { …}

ECE453 & CS447 Tutorial-3 Expect Basics 9


The * Wildcard
The * match longest string from > expect "hi*" {
input. send "$expect_out(0,string)--";
send $expect_out(buffer) }
The * matches “losophical” philosophical //input from keyboard
hilosophical //value of expect_out(0,string)
--
philosophical //value of expect_out(buffer)
The * matches “losop”
>
> expect “hi*hi” … //same as above
philosophical //input from keyboard
The first * matches “philosop” Hilosophi-- //expect_out(0,string)
The second * matches “cal” philosophi //expect_out(buffer)
>
> expect “*hi*” … //same as above
Using expect "*" match everything, philosophical //input
including empty string. All input philosophical //expect_out(0,string)
current available is thrown away. --philosophical //expect_out(buffer)
>
ECE453 & CS447 Tutorial-3 Expect Basics 10
Usage of Backslashes (1)
• Rules of backslash:
– Tcl shell translate backslash string first in its way.
– Expect pattern matcher evaluates translated result.
– Backslash disable the function of wildcard.
– Backslash does nothing for non-special characters.
• Examples
– expect “\n”; #matches \n (linefeed char)
– expect “\r”; #matches \r (return char)
– expect “\z”; #matches z (literal z)
– expect “\{”; #matches { (literal left brace)
– expect “*”; #matches everything
– expect “\\*”; #matches literal *
equivalent
– expect “\*”; #matches what?
ECE453 & CS447 Tutorial-3 Expect Basics 11
Usage of Backslashes (2)
• In the pattern matcher, linefeed character is not same as
\n in Tcl interpreter. The pattern matcher never translates
string “\n” to linefeed. Translation has been done by Tcl.
• Examples
– expect “n”; #matches literal n
– expect “\n”; #matches \n (linefeed char)
– expect “\\n”; #matches n
– expect “\\\n”; #matches \n (linefeed char)
– expect “\\\\n”; #matches sequence of \ and n
– expect “\\\\\n”; #matches sequence of \ and \n (linefeed)
– expect “\\\\\\n”; #matches sequence of \ and n
– expect “\\\\\\\n”; #matches sequence of \ and \n (linefeed)
– expect “\\\\\\\\n”; #matches sequence of \ and \ and n
ECE453 & CS447 Tutorial-3 Expect Basics 12
Usage of Backslashes (3)
• The “[“ is special for Tcl and pattern matcher.
– It can be command execution prefix for Tcl
– It can be range matching pattern for expect
– It can be common literal in a pattern
• Examples
let XY denote a procedure which return a string “n*w”
– expect “[XY]”; #matches n followed by anything followed by w.
– expect “\[XY]”; #matches literal X or Y
– expect “\\[XY]”; #matches n followed by anything followed by w.
– expect “\\\[XY]”; #matches sequence [XY]
– expect “\\\\[XY]”; #matches \ followed by n followed by …
– expect “\\\\\[XY]”; #matches sequence of \ followed by X or Y

ECE453 & CS447 Tutorial-3 Expect Basics 13


Handling Error and Timeout
Using timeout and other patterns to …
detect errors when establishing set timeout 60 //getting file takes time
connection and using exit to spawn ftp somewhere
terminate the execution of script. expect {
timeout {puts “timed out”; exit}
“connection refused” {exit 1}
“unknown host” {exit 1}
“Name”
}
Matches a new-line followed by a send “anonymous\r”
error code 4 or 5. …
expect {
timeout {unexpected…}
“\n\[45]” {errors… }
Matches the current buffered
“^[45]” {errors… }
string start with 4 or 5. If there are
“ftp> ”
unread string left after last expect,
}
this may cause errors.

ECE453 & CS447 Tutorial-3 Expect Basics 14


Handling End of File (eof)
• Two cases:
– When the spawned process closes its connection to Expect, Expect
sees an eof.
– When expect closes its connections, spawned process sees an eof and
“hangup” signal.
• close command
– Whenever one side closes connection, the other side should close
connection as.
– Expect uses close command to close its connection.
– In most case, expect closes connection automatically.

expect {
Using eof to catch the
timeout {puts “timed out”; exit}
connection closing event.
eof {do some actions}

}
ECE453 & CS447 Tutorial-3 Expect Basics 15
Regular Expressions (regexp)
• Regular expression is more powerful than Glob pattern.

Patterns in common Special in regexp


glob regexp Meaning regexp Meaning
s s literal s [^a-z] Any char not in the range
^ ^ beginning of string a* any number of “a”
$ $ end of string a+ Non-empty “a” sequence
[a-z] [a-z] range matching [0-9]* any decimal sequence
? . any single character a? Match “a” or “” only
* .* any string | Match any of branches

• Example of matching any decimal, hex and octal.


“-?[1-9][0-9]*|0x[0-9a-fA-F]+|0[0-7]*”

ECE453 & CS447 Tutorial-3 Expect Basics 16


Identify Patterns
• Identify regexp and glob pattern
– The default pattern in expect is glob pattern.
– Using –re and –gl to identify regexp and glob pattern
expect –re “a*” # match “”, “a”, “aa”, …
expect “a*” # match sequence of a followed by anything
expect “- gl a*” # same as line above.
• Mixed matching patterns
expect {
#using regexp –re “a*” {action 1}
#using glob pattern “b*” {action 2}
}
• Using regexp
– Backslash any characters that are special to Tcl.
expect –re “-?\[1-9]\[0-9]*|0x\[0-9a-fA-F]+|0\[0-7]*”

ECE453 & CS447 Tutorial-3 Expect Basics 17


Using Parenthesis in Pattern
• Parentheses are used to group sub-pattern
– Regexp: ab+ matches “ab”, “abb”, “abbb”… not “abab”
– To match “ababab”, using (ab)+
– It is dangerous to use (ab)*, since it matches “”. expect will not wait
for any input and directly terminate.

• Using parentheses for feedback


– When the regexp successfully matches a string, each part of the string
that matches a parenthesized sub-pattern is saved in the array
expect_out. The first is stored in expect_out(1,string), the second in
expect_out(1,string)…
– Assume input buffer is “junk abcbcd” and matching pattern “a(.*)c”
expect_out(0,string) has “abcbc” #entire matched string
expect_out(1,string) has “bcb” #string matches (.*)
expect_out(buffer) has “junk abcbc” #all string buffer read so far.

ECE453 & CS447 Tutorial-3 Expect Basics 18


Pattern Matching Rules of Regexp
• Rule 1: a regexp matches at the first possible position in the string
– Pattern: “a?b”. Input buffer: “ba”.
– Matching result: “b”
• Rule 2: the left-most matching branch is used
– Pattern: “a|ab”. Input buffer: “ab”.
– Matching result: “a”
• Rule 3: the longest match is used
– Pattern: “a*b”. Input buffer: “abababa”.
– Matching result: “ababab”
• Rule 4: sub-expressions are considered from left to right
– Pattern: “a*(b*|(ab)*)”. Input buffer: “aabab”.
– Matching result: “aab”
– In above example, is it possible to match the first “a” with the pattern
a*, and use sub-pattern “(ab)*” to match remaining “abab”?
ECE453 & CS447 Tutorial-3 Expect Basics 19
Feedback of Nested-Parentheses
• Determine stored position in the expect_out array of sub-pattern

expect_out(buffer)
expect_out(0,string)
expect_out(1,string)
expect_out(2,string)

(.*(a*((ab)*|b*)))

• Count the left parentheses to determine feedback position: the sub-


expression which starts with the Nth left parenthesis corresponds with
expect_out(N, string).

ECE453 & CS447 Tutorial-3 Expect Basics 20


Backslashes for Regexp
• Backslash any characters that are special to Tcl when
using Regexp.
• Examples
“$” is special for both Tcl and regexp pattern of expect.
Assume variable a has value “x”
– expect –re “$a”; #match x.
– expect –re “\$a”; # match string end with literal a.
– expect –re “\\$a”; # match literal x.
– expect –re “\\\$a”; # match sequence $ followed by a.
– expect –re “\\\\$a”; # match sequence \ followed by x.
– expect –re “\\\\\$a”; # match sequence \ followed by $ followed by a
– expect –re “\\\\\\$a”; # match sequence \ followed by x
– expect –re “\\\\\\\$a”; # match ?

ECE453 & CS447 Tutorial-3 Expect Basics 21

You might also like