120 likes | 370 Views
Expect. Tool for Automation and Testing. Expect - Introduction. Unix automation and testing tool Written by Don Libes Primarily designed as an extension to the Tcl scripting language
E N D
Expect Tool for Automation and Testing
Expect - Introduction • 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 • 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 • Real & Int cast to real • Non-numeric string and real/int cast to string • Built in Lists • Standard flow control structures (while, if, etc.) • String manipulation with Regular Expressions • Procedures – arguments can be passed by value or reference *Tool Command Language
Expect – The Programming Language • Extension of TCL, so the syntax is TCL syntax • Basic command set • expect • send • spawn • Interact • Regular Expression pattern matching • “Glue” to combine separate executables
Expect – Pros / Cons • Pros • 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, ComputerVision 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 cross-platform is not easily supported.
Expect – “Hello World!” Start Expect with “expect” or write the script to a file and run > expect filename • send “Hello World!\n” • expect “Hi\n” Putting it together • Expect “Hi\n” { send “Hi World!\n” }
Expect – More Advanced • expect "hi\n" { send "Hi World!\n" } \ "hello\n" {send "Hello World!\n" } \ "bye\n" {send "Bye World!\n" }
Expect - Automation spawn ftp $argv expect "Name" { send "anonymous\r" } \ "name" { send "anonymous\r" } expect "assword:" send "nobody@nobody.com\r" interact
Expect - Others • Brute Force security • Beer • Chess • Weather • Rogue • Hunt
# start things rolling spawn gnuchess set id1 $spawn_id expect "White \\(1\\) :" send "random\r" send "depth 6\r" send "go\r" # read_first_move expect -re "My move is : (.*)\n" spawn gnuchess set id2 $spawn_id expect "White \\(1\\) :" send "random\r" send "depth 5\r" send $expect_out(1,string) while {1} { expect { -i $id2 -re "is : (.*)\n" { send -i $id1 $expect_out(1,string) } -i $id1 -re "is : (.*)\n" { send -i $id2 $expect_out(1,string) } } } Expect - GnuChess