• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Expect the unexpected
 

Expect the unexpected

on

  • 2,176 views

WebTech Varna 2007

WebTech Varna 2007

Statistics

Views

Total Views
2,176
Views on SlideShare
2,176
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Expect the unexpected Expect the unexpected Presentation Transcript

    • WebTech Varna 2007Expect – the unexpected Marian Marinov – mm@yuhu.bizSystem Architect - Siteground.com
    • Agenda ➢ What is Expect and where it can help ➢ Implementations ` ➢ UNIX tool expect ➢ Python pexpect ➢ Perl Expect ➢ PHP expect 2Marian Marinov - mm@yuhu.biz 1/30 WebTech Varna 2007
    • What is Expect ➢ Automating Interactive Applications ➢ Unique System Administrators tool ➢ Just Another TCL extension ➢ Great automated application tester Where to use it: ➢ passwd automations ➢ automation of ncurses based application ➢ automated patch management 3Marian Marinov - mm@yuhu.biz 2/30 WebTech Varna 2007
    • UNIX Expect application Requirements: tcl & expect Don Libes - http://expect.nist.gov/ hackman@gamelon:~$ expect expect1.1> send "hello world" hello worldexpect1.2> exit hackman@gamelon:~$ expect expect1.1> send "hello worldn"; hello world expect1.2> exit 4Marian Marinov - mm@yuhu.biz 3/30 WebTech Varna 2007
    • UNIX Expect application hackman@gamelon:~$ cat speak.exp #!./expect -f send "hello worldn" hackman@gamelon:~$ ./speak.exp hello world 5Marian Marinov - mm@yuhu.biz 4/30 WebTech Varna 2007
    • UNIX Expect application send – sending string to the command send_user – sending string to STDOUT send_log – sending string to text log file set timeout -1|0|60 – maximum wait for command input stty -echo – turn off KBD echo spawn – start a program interact – returns the control of the started command back to user 6Marian Marinov - mm@yuhu.biz 5/30 WebTech Varna 2007
    • UNIX Expect application match_max 100000 - max buffer size log_user 0|1 – enable/disable logging to STDOUT log_file filename – file where to store the script output as addition to STDOUT expect -exact – match exact string expect -re – match regular expression expect eof – wait the command to finish 7Marian Marinov - mm@yuhu.biz 6/30 WebTech Varna 2007
    • UNIX Expect application expect { # nestead expectes -re "" { } -exact "" { } -re "" { } } 8Marian Marinov - mm@yuhu.biz 7/30 WebTech Varna 2007
    • UNIX Expect application Get some user input: stty -echo expect_user -re "(.*)n" set password $expect_out(1,string) send_user "n" 9Marian Marinov - mm@yuhu.biz 8/30 WebTech Varna 2007
    • UNIX Expect application Show some examples :) 10Marian Marinov - mm@yuhu.biz 9/30 WebTech Varna 2007
    • UNIX Expect application Im stupid and I didnt understood anything Can I still use Expect? YES autoexpect 11Marian Marinov - mm@yuhu.biz 10/30 WebTech Varna 2007
    • UNIX Expect application autoexpect ftp ftp.example.com autoexpect -f ftp.exp ftp ftp.example.com -c – conservative mode -p – prompt mode 12Marian Marinov - mm@yuhu.biz 11/30 WebTech Varna 2007
    • Python pexpect package URL: http://pexpect.sourceforge.net/ Requirements: python2.4 & pty package Installation: 1. download pexpect-2.1.tar.gz 2. tar zxf pexpect-2.1.tar.gz 3. cd pexpect-2.1 4. python setup.py install 13Marian Marinov - mm@yuhu.biz 12/30 WebTech Varna 2007
    • Python pexpect package import pexpect child = pexpect.spawn (ftp ftp.openbsd.org) child.expect (Name .*: ) child.sendline (anonymous) child.expect (Password:) child.sendline (test@example.com) child.expect (ftp> ) child.sendline (cd pub) child.expect(ftp> ) child.sendline (get ls-lR.gz) child.expect(ftp> ) child.sendline (bye) 14Marian Marinov - mm@yuhu.biz 13/30 WebTech Varna 2007
    • Python pexpect package Regular expression problems: matching the end of the line can be tricky :) The $ pattern for end of line match is useless. child.expect (w+rn) - matching word at the end of the line minimal match(instead of the standard greedy): child.expect (.+) - match always one char child.expect (.*) - match always zero or one char 15Marian Marinov - mm@yuhu.biz 14/30 WebTech Varna 2007
    • Expect for Perl Packages: Expect Expect::Simple Installation: perl -MCPAN -e install Expect perl -MCPAN -e install Expect::Simple Download and compile (http://search.cpan.org) 16Marian Marinov - mm@yuhu.biz 15/30 WebTech Varna 2007
    • Expect for Perl Expect->new(); $object->spawn(@command); $object->debug($debug_level); $object->exp_internal(0 | 1); $object->raw_pty(0 | 1); 17Marian Marinov - mm@yuhu.biz 16/30 WebTech Varna 2007
    • Expect for Perl $object->match(); $object->matchlist(); $object->match_number(); $object->error(); $object->command(); $object->exitstatus(); $object->do_soft_close(); $object->restart_timeout_upon_receive(0 | 1); 18Marian Marinov - mm@yuhu.biz 17/30 WebTech Varna 2007
    • Expect for Perl $object->log_group(0 | 1 | undef); $object->log_user(0 | 1 | undef); $object->log_file("filename" | $filehandle | &coderef | undef); $object->match_max($max_buffersize or undef); $object->pid(); $object->send_slow($string_to_send); $object->send_slow($delay, @strings_to_send); 19Marian Marinov - mm@yuhu.biz 18/30 WebTech Varna 2007
    • Expect in PHP Requirements: libexpect >= 5.43.0 PHP >= 4.3.0 PECL Installation: pear install expect 20Marian Marinov - mm@yuhu.biz 19/30 WebTech Varna 2007
    • Expect in PHP ini_set ("expect.loguser", "Off"); $stream = fopen ("expect://ssh root@remotehost uptime", "r"); $cases = array ( array (0 => "password:", 1 => PASSWORD) ); 21Marian Marinov - mm@yuhu.biz 20/30 WebTech Varna 2007
    • Expect in PHP switch (expect_expectl ($stream, $cases)) { case PASSWORD: fwrite ($stream, "passwordn"); break; default: die ("Error was occurred while connecting to the remote host!n"); } while ($line = fgets ($stream)) { print $line; } fclose ($stream); 22Marian Marinov - mm@yuhu.biz 21/30 WebTech Varna 2007