expect notes

(draft)

Expect Language, Expect Book.

Summary

  • set timeout x (in seconds)
  • spawn
  • expect
  • send

Example of Using with Perl and Expect.pm

#!/usr/bin/perl -w
use Expect;
my %accounts = ('user' => 'secret' );
foreach my $key (sort keys %accounts ) {
my $password = $accounts{$key};
my $exp = Expect->spawn('ssh root@host.foo.bar');
$exp->expect(2,[ qr/mailer/i, sub { my $self = shift; $self->send("adduser $keyn"); }] );
$exp->send("passwd $keyn");
$exp->expect(2, [ qr/password:/i, sub { my $self = shift; $self->send("$passwordn"); }] );
$exp->expect(2, [ qr/password:/i, sub { my $self = shift; $self->send("$passwordn"); }] );
$exp->expect(2, [ qr/bar/i ] ); $exp->hard_close();
}

Actions – Pattern-Command Pairs

Patterns can be directly associated with Commands – “Actions”.

expect {
eof {break}
timeout {exit}
"fred" {send "flintstoner"}
"denied" {close}
"\? " {interact +}
}

  • interact – returns the keyboard to the user – in the above example typing the + key then returns to expect
  • be aware of r vs n – use r with send, use rn when looking for an end-of-line in patterns
  • eof – spawned program exits
  • timeout – spawned program doesn’t respond within time
  • default = eof + timeout
  • close – close spawned program: do when you don’t want to wait for spawned program to eof and you want to do more in the script
Tags: ,


Share This


 


One Response to expect notes

  1. Nice site keep it up!

    ————————————–
    http://www.dasofte.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>