expect notes
| 14-Nov-2007 | Posted by Sonia Hamilton under Expect, Perl |
(draft)
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
Share This
Nice site keep it up!
————————————–
http://www.dasofte.com