8/8/2019 Expect Course
1/34
E x p e
c t Gr e gB
a k e r
gr e g b @i f o s t . or g . a
u
8/8/2019 Expect Course
2/34
Wh a t i s i t ?
l
-Al a n g
u a g e f or a u t
om a t i n gi n t
e r a c t i v e
pr o gr a m s
l
-AT
c l e x t e n s i on
l
-P u b l i c
d om a i n
l
- N a m e d a f t e r i t s m
o s t u s e f ul c omm a n d
8/8/2019 Expect Course
3/34
V er s i on s
l
- V e r s i on 5 . 3 1
w or k s wi t h T
c l 8 .2 ( 2 2
O c t o b e r 1
9 9 9 )
l
- V e r s i on 5 . 3 1 . 8 ( 5 D
e c 2 0 0 0 )
8/8/2019 Expect Course
4/34
U s ef ul E x p e c t
ex am pl e s
l
- p a s s m a s s
l
-k i b i t z
l
- t i m e d -r e a d , t i m e d -r un
l
- d i s l o c a t e
8/8/2019 Expect Course
5/34
D o c um
en t a t i on
l
- e x p e c t ( 1 ) m
a n p a g e
l
-" E x pl or i n gE x p e c t " b yD
onL i
b e s
( O' R
e i l l y a n d A
s s o c i a t e s ,
I S B
N1 - 5 6 5 9 2 - 0 9 0 -2 ) .
8/8/2019 Expect Course
6/34
F o ur / f or em
o s t i m p or t an t
c omm
an d s
l
- [ s p a wn
] s t a r t a pr o gr a m
l
- [ e x p e c t ] w a i t f or s om e i n p u t
l
- [ s e n d ] s e n d s om e t h i n g
t o t h e pr o gr a m
l
- [ i n t e r a c t ] a l l o w u s e r i n t e r a c t i on t h r o u gh
8/8/2019 Expect Course
7/34
A u t om a t i n gf t p
f
t p l
o c a
l h
o s t
C
o nn
e c t e d
t o
l
o c a
l h
o s t .
2
2 0
j a y a
n y a .
i f
o s t . o
r g . a u
FTP
s e
rv
er r
e
a d y .
N
a m e
(
l o c a
l h
o s t : g
r e g b ) :
u s er1
3
3 1 P
a s s
w o
r d
r
e q u
i r
e d
f
or
u s e
r1
.
P
a s s
w o
r d :
2
3 0 -
j a y a
n y a
p p c
2
3 0
U s e
r
u s e
r1 l
o g g e d
i n
.
R
e m o t e
s y s t e
m
t y p e
i
s
UNIX
.
U
s i n
g
b i n
ar
y m
o d e
t o
t
r a
n s
f er f i l
e s .
f
t p> d i r
8/8/2019 Expect Course
8/34
T h ei n t er a c t i on
s n e c e s s ar y ar e:
l
-T
o p u t i n a
u s e r n a m e
l
-T
o p u t i n a
p a s s w
or d
l
-T
or un
s om e c om
m a n d s
8/8/2019 Expect Course
9/34
E x am pl eP r o g
# ! / u s
r / b
i n
/ e
x p e c t
s p a
wn
f t p
l
o c a
l h
o s
t
ex
p e c t
" N
am
e"
;
s e
n d
" u s e
r1
\ n"
ex
p e c t
" P
a s s
w o
r d :
"
s e
n d
" c
l a s s
1 \
n"
ex
p e c t
" f
t p
>
s e
n d
" p u t
m
yf i l
e
m
y f i l
e . b a
k
\ n"
ex
p e c t
" *
b y t e s
s e
n t
i n
* f
t
p >
s e
n d
" b y e \
n"
8/8/2019 Expect Course
10/34
T c l / E x p
e c t S y n t ax
l
-n or m
a l l y on e - p e r -l i n e
l
- ( i . e . t e r mi n a t e d
b y a n e w-l i n e
c h a r a c t e r )
l
- C a n b e s e p a r a t e d wi t h
s e mi - c ol on s
8/8/2019 Expect Course
11/34
P r o b l em s
l
- Wh a t i f w
e c a n' t r e a c h t h e F T P s e r v e r ?
l
- Wh a t i f t h e
p a s s w
or d i s
wr on
g ?
l
- Wh a t i f t h e
c onn e c t i on t i m e s o u t ?
l
-I t ' s
a b i t t a l k a t i v e
8/8/2019 Expect Course
12/34
M ul t i - p a t t er n ex p e c t
e
x p e c t
p a t t e
rn
a c t
i o
n
p a t t e
rn
a
c ti
on
. . .
l
- U s e { . . . } or " . . ." f
or a c t i on s or p a t t e r n s
wi t h
s p a c e s
l
-I f t h e i n
p u t b uf f e r m a t c h e s
a p a t t e r n , t h e
p a i r e d a c t i oni s r un
8/8/2019 Expect Course
13/34
T h e t h r e ek e y w or d s f or p a t t er n
s
l
[ e of ] T o b e r un on e n d - of -f i l e
( e . g . t h e s p a wn e d
pr o gr a m c om pl e t e s )
l
[ t i m e o u t ] W a i t N s e c on d s f or i n
p u t , t h e n
t r y t h i s
( N=
t h e " - t i m e o u t "
p a r a m e t e r or
" t i m e o u t " v a r i a b l e )
l
[ d e f a ul t ] E i t h e r of t h
e t w o a b o v e s c e n a r i o s .
8/8/2019 Expect Course
14/34
I nf or mi n
g t h e u s er
l
- s e n d _ u s e r " W or k i n g ! \ n"
( s t a n d a r d o u t p u t )
l
- s e n d _ e r r or " F a i l e d
. \ n"
( s t a n d a r d e r r or )
l
- s e n d _ t t y" M e s s a g e \ n"
( c on t r ol l i n g
t e r mi n a l )
8/8/2019 Expect Course
15/34
T c l v ar i a b l e s
l
- s e t u s e r n a m
e " u s e r 1 "
l
- s e n d "
$ u s e r n a m e \ n"
8/8/2019 Expect Course
16/34
O t h er t h i n g s
l
[ l o g _ u s e r 0 ]
D
on' t pr i n t t h e s e s s i on t r a n s c r i p t
l
[ e x p e c t -i ]
L
o ok f
or t h e p a t t e r n c a s e -i n s e n s i t i v e l y
l
[ e xi t ]
E xi t pr
o gr a m wi t h
a p pr o pr i a t e e r r or c o d e
8/8/2019 Expect Course
17/34
s e t
u s e r
n a
m e
"
u s e
r
1
;
s e t
p a s s
w o
r d
"
cl
a s s
1
; l
o g _ u s e
r
0
s p a
wn f t
p l
o c a
l h
o s
t
s e t
t
i m e
o u t
4
5
ex
p e c t
" 2 2
0 * N
am
e"
{ s e
n d _ u s e
r "
C o
nn
e c t e d ! \
n"
}
\
t
i m
e o u t
{
s e
n d _ e
r
r or " N
ev
er
g o t
n
am
e \
n"
;
e xi
t }
s e
n d
"
$ u s e
rn
am
e \
n"
ex
p e c t
-i " P
a s s
w o
r
d "
s e
n d
"
$ p a s s
w o
r d \
n"
ex
p e c t
" 5 3 0
L
o g
i n
i n c o
rr
e c t
"
{
s e n d _ e
rr
or " B
a d
p a s s
w o
r d \
n"
;
exi
t 2
} \
" 2
3 0
* l o
g g e d
i n* f
t
p >"
{
s e
n d _ u s e
r " P
a s s
w o
r d
OK
\ n"
}
\
ti m e
o u t
{
s e
n d
_ err
or " L
o g
i n
ti m
e d
o u t
"
; e
xi
t
3
}
s e t
t
i m e
o u t
2
0 0
;
#
c o u
l d
t a
k e
a
wh i l
e
t o
s e
n d
s e
n d
"
p u t
m
yf i l
e m
y f i l
e . b a
k \
n"
ex
p e c t
" 2 2
6 Tr
an
sf
e r
c o
m p
l e t e
* f
t p
>"
{
s e
n d _ u s e
r " Tr
a
n sf
er
OK
\ n"
}
\
ti m e
o u t
{
s e
n d
_ err
or " Tr
an
sf
er n
o t
d o
n e
i n
ti m
e"
}
s e
n d
"
b y e \
n"
8/8/2019 Expect Course
18/34
E x er c i s eB r e ak
1 . A
u t om
a t e a t e l n e t c onn e c t i on
l o gi n
2 . U
s e i t
t o g e t a d i r e c t or yl i s t i n g
3 . T r a n s f e r i t t o
t r a n s f e r a f i l e
( h i n t : c a t f i l e ; e c h o" Z Z Z -E A
S Y-T
O-RE
C O G NI S E "
)
4 . ( O
p t i on a l ) T e s t wh
e t h e r a n e m a i l
a d d r e s s i s v a l i d
8/8/2019 Expect Course
19/34
T c l / E x p
e c t S y n t ax
l
- e v e r y t h i n gi s
a s t r i n g
l
- c omm
a n d s
b r e a k i n
t o" w or d s " ( l i k e t h e
Uni x
s h e l l )
l
-f i v e s p e c i a l c h a r a c t e r s :
l
{ . . . } "
. . ." [ . . . ] $ \
8/8/2019 Expect Course
20/34
{ .... }
l
a c t s l i k e
Uni x
s h e l l s i n gl e - q u o t e
( ' )
l
c a n b e n e
s t e d
l
c a n s pr e a d o v e r s e v e r a l l i n e s
( c omm
on )
8/8/2019 Expect Course
21/34
" ...."
l
" . . . ." a c t s l i k e Uni x
s h e l l d o u b l e - q u o t e s
l
$ v a r g e t s r e pl a c e d wi t h
c on t e n t s of " v a r "
l
u s u a l s t r i n g
c h a r s \ n \ t \ a \ r
l
c a n s pr e a d o v e r s e v e r a l l i n e s
8/8/2019 Expect Course
22/34
[ .... ]
l
a b i t l i k e Uni x s h e l l b a c k q u o t e s
l
r un t h e c omm a n d b e t w e e n [ a n d ]
l
c omm
a n d o u t p u t i s a c omm
a n d
a r g um e n t
8/8/2019 Expect Course
23/34
S t y l i s t i c i m pr ov
em en t s
e x p e c t {
" 2 2 0 * N a m e "
{
s e n d _ u s e r " C onn e c t e d ! \ n"
} t i m e o u t {
s e n d _ e r r or " N
e v e r g o t n a m e \ n" ; e xi t 1
} }
8/8/2019 Expect Course
24/34
Wh a t c omm
an d s ar e t h er e?
l
l o o pi n g ,l i s t s ,h a s h / a r r a y s
l
pr o c e d ur e s ,f un c t i on s
l
i nf
o
c o
mm a
n d s
l
c omm
a n d s t h e m s e l v e s m a y gi v e s om e
u s a g e i nf or m
a t i on
l
t r i e s a u t o _ l o a d
8/8/2019 Expect Course
25/34
U s er i n t
er a c t i on
l
e x p e c t _ u s e r
l
i n t e r pr e t e r
l
i n t e r a c t
8/8/2019 Expect Course
26/34
ex p e c t _ u s er
l
s a m e a s n
or m a l e x p e c t
l
r e a d s f r om u s e r r a t h e r t h a n
pr o c e s s
8/8/2019 Expect Course
27/34
i n t er pr e t er
l
s t a r t s a n
e x p e c t i n t e r pr e t e r
l
u s e f ul f or d e b u g gi n g
l
v e r y u s e f ul wi t h i n t e r a c t . . .
8/8/2019 Expect Course
28/34
i n t er a c t
s
p awn
f t p
l o
c a
l h
o s t
e
x p e c t
" N
am e"
;
s e
n d
" u s e
r1
\ n"
e
x p e c t
" P
a s s
w o
r d :
"
s
e n d
" c
l a s s 1 \
n"
e
x p e c t
f t p
>
i
n t e
r a c t
{
rm
{
s e n d
d e
l e t e }
~ c
{
s e n d
" m
g e t
* . c
"
}
~ p
{
s e n d
" m
g e t
* . p
l "
}
}
8/8/2019 Expect Course
29/34
m ul t i pl e s e s s i on s
l
s p a
wn
s e t s t h e v a r i a b l e " s
p a
wn
_ i d
"
l
s a v e s p a
wn _i
d a n d
s p a wn
a g a i n
l
u s e e
x p e
c t -i
a n d s
en
d -i
8/8/2019 Expect Course
30/34
s p a
wn
t el n
e t
h
o s t
1
;
s e t
s e s s
i o
n1
$ s p a
wn
_i
d
;
i n
t e
r a
c t " ^ -" r
e t u
rn
; #
m
an
u a
l l o
gi n
s p a
wn
t el n
e t
h
o s t
2
;
s e t
s e s s
i o
n2
$ s p a
wn
_i
d
i n
t e
r a
c t " ^ -" r
e t u
rn
; #
m
an
u a
l l o
gi n
s e t
t
i
m e o u t
-1
; #
n
o
ti m
e o u t
wh i l
e
( 1 )
{
s e
n d _
u s e
r "
\ ni
d ,
l
s ,
d a t e
o
r
q> "
e
x p e c
t {
-i
$
u s e
r _ s p a
wn
_i
d
" i
d
\ n"
{
s e
n d
-i
$ s e s s
i o
n1 " i d
\ n"
;
s e
n d
-i
$ s e s s
i o
n2 " i d
\ n"
}
" l
s
\ n"
{
s e
n d
-i
$ s e s s
i o
n1 " l s
\ n"
;
s e
n d
-i
$ s e s s
i o
n2 " l s
\ n"
}
" d a
t e \
n"
{
s e
n d
-i
$ s e s s
i o
n1 "
d a t e \
n"
;
s e
n d
-i
$ s e s s
i o
n2 "
d a t e \
n"
}
" q
"
{
b r
e a
k
}
" \
n
" {
}
-i
$
s e s s
i o
n1 -r
e "
.+"
{
s e
n d _ u s e
r "
\ n* * 1 >
$ e
x p
e c t _ o u t ( b u
f f
er
) "
}
-i
$
s e s s
i o
n2 -r
e "
.+"
{
s e
n d _ u s e
r "
\ n* * 2 >
$ e
x p
e c t _ o u t ( b u
f f
er
) "
}
}
}
8/8/2019 Expect Course
31/34
E x er c i s e t i m e
1 .I m
pr o v e t h e F T P
e x a m pl e f r om- t h e
u s e r
s h o ul d i n t e r a c t t of i x a wr on g
p a s s w or d
2 .E x t e n d F T P
t o c o p yf r om
s y s A
t o s y s B
vi a y o ur m a c h i n e
3 . F i x
d o u b l e -h e a d e d - t e l n e t t o :
a ) n o t s h o w t h e s h e l l pr om
p t
b ) i n t e r a c t a g a i n wi
t h a s e s s i on
8/8/2019 Expect Course
32/34
W al k t h r o u gh
of T c l ex am pl e s
l
s a m pl e t c l - b a s i c . e x p
l
s a m pl e t c l - a d v a n c e d . e x p
8/8/2019 Expect Course
33/34
M o d ul ar pr o gr ammi n g
l
s o ur c e f i l
e n a m e
l
n a m e s p a c e
8/8/2019 Expect Course
34/34
E x er c i s e
l
R e m o v e y o ur m o s t t i m e - w a s t i n g t a s k