15 Nov 2007
(draft)
Summary
- set timeout x (in seconds)
- spawn
- expect
- send
Example of Using with Perl and Expect.pm
#!/usr/bin/perl -w<br />
use Expect;<br />
my %accounts = ('user' => 'secret' );<br />
foreach my $key (sort keys %accounts ) {<br />
my $password = $accounts{$key};<br />
my $exp = Expect->spawn('ssh root@host.foo.bar');<br />
$exp->expect(2,[ qr/mailer/i, sub { my $self = shift; $self->send("adduser $keyn"); }] );<br />
$exp->send("passwd $keyn");<br />
$exp->expect(2, [ qr/password:/i, sub { my $self = shift; $self->send("$passwordn"); }] );<br />
$exp->expect(2, [ qr/password:/i, sub { my $self = shift; $self->send("$passwordn"); }] );<br />
$exp->expect(2, [ qr/bar/i ] ); $exp->hard_close();<br />
}<br />
Actions – Pattern-Command Pairs
Patterns can be directly associated with Commands – “Actions”.
expect {<br />
eof {break}<br />
timeout {exit}<br />
"fred" {send "flintstoner"}<br />
"denied" {close}<br />
"\? " {interact +}<br />
}
- 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