Expect Tool for Automation and Testing Expect Introduction

  • Slides: 11
Download presentation
Expect Tool for Automation and Testing

Expect Tool for Automation and Testing

Expect - Introduction l l Unix automation and testing tool Written by Don Libes

Expect - Introduction l l Unix automation and testing tool Written by Don Libes Primarily designed as an extension to the Tcl scripting language Useable in interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others.

Expect – TCL* Basics l l Everything is a command No Fixed Parser Case-Sensitive

Expect – TCL* Basics l l Everything is a command No Fixed Parser Case-Sensitive Explicit Expression Evaluation – – – set a 5 a = 5 set b a+2 b = a+2 set c [expr $a+2] c = 7 (Note: $ for variable substitution) Simple variables are strings of arbitrary length l Real & Int cast to real l Non-numeric string and real/int cast to string l Built in Lists l Standard flow control structures (while, if, etc. ) l String manipulation with Regular Expressions l Procedures – arguments can be passed by value or reference *Tool Command Language l

Expect – The Programming Language l l Extension of TCL, so the syntax is

Expect – The Programming Language l l Extension of TCL, so the syntax is TCL syntax Basic command set – – l l expect send spawn Interact Regular Expression pattern matching “Glue” to combine separate executables

Expect – Pros / Cons l Pros – – – l Built on existing

Expect – Pros / Cons l Pros – – – l Built on existing system tools, so learning curve is small. Extensive support from corporate entities* *Silicon Graphics, IBM, HP, Sun, Xerox, Amdahl, Tektronix, AT&T, Computer. Vision and the World Bank Numerous ports to other programming languages (Perl, Python, java, etc. ) Cons – – – Only useful for command line scripting Cryptic syntax for those unfamiliar with TCL (When do I use a $ again? ) Generally machine dependent tools are run with Expect, so porting scripts crossplatform is not easily supported.

Expect – “Hello World!” Start Expect with “expect” or write the script to a

Expect – “Hello World!” Start Expect with “expect” or write the script to a file and run > expect filename l send “Hello World!n” l expect “Hin” Putting it together l Expect “Hin” { send “Hi World!n” }

Expect – More Advanced l expect "hin" { send "Hi World!n" }  "hellon"

Expect – More Advanced l expect "hin" { send "Hi World!n" } "hellon" {send "Hello World!n" } "byen" {send "Bye World!n" }

Expect - Automation spawn ftp $argv expect "Name" { send "anonymousr" }  "name"

Expect - Automation spawn ftp $argv expect "Name" { send "anonymousr" } "name" { send "anonymousr" } expect "assword: " send "nobody@nobody. comr" interact

Expect - Others l l l Brute Force security Beer Chess Weather Rogue Hunt

Expect - Others l l l Brute Force security Beer Chess Weather Rogue Hunt

Expect - Gnu. Chess # start things rolling spawn gnuchess set id 1 $spawn_id

Expect - Gnu. Chess # start things rolling spawn gnuchess set id 1 $spawn_id expect "White \(1\) : " send "randomr" send "depth 6r" send "gor" # read_first_move expect -re "My move is : (. *)n" spawn gnuchess set id 2 $spawn_id expect "White \(1\) : " send "randomr" send "depth 5r" send $expect_out(1, string) while {1} { expect { -i $id 2 -re "is : (. *)n" { send -i $id 1 $expect_out(1, string) } -i $id 1 -re "is : (. *)n" { send -i $id 2 $expect_out(1, string) } } }

Expect - Questions

Expect - Questions