Also sprach J Krugman:
Expect.pm may be great if you, unlike me, have prior experience
using /usr/bin/expect. Otherwise, I found it to be rough going,
and not worth the aggravation. The documentation didn't make much
sense and the examples given in the tutorial did not work (as
acknowledged in the Expect.pm FAQ).
The secret about Expect seems to be to know which of the many methods
one will most likely never need. So far I always got away with using
send() and expect().
I am using the following script to refresh the password of one of my ssh
accounts (policy requires to change it every six weeks). Since I cannot
be bothered to come up and remember new passwords all the time, I used
Expect to log in and cycle the password a bit until the original
password is set again:
my $exp = Expect->spawn("ssh", qw(-l tasvon cagney.informatik.rwth-aachen.de))
or die "Cannot spawn process: $!\n";
$exp->expect(5, 'password:');
$exp->send("origpwd\n");
$exp->expect(5, 'tasvon@cagney:~\$/');
my @pwd = qw(origpwd abc123 bcd234 cde456 origpwd);
for (0 .. $#pwd-1) {
$exp->send("passwd\n");
$exp->expect(5, 'Geben Sie ihr altes Kennwort ein:');
$exp->send("$pwd[$_]\n");
$exp->expect(5, 'Geben Sie ihr neues Kennwort ein:');
$exp->send("$pwd[$_+1]\n");
$exp->expect(5, 'Wiederholen Sie ihr neues Kennwort:');
$exp->send("$pwd[$_+1]\n");
!defined($exp->expect(10, 'Ihr Kennwort wird geaendert.')) and die "failed\n";
}
$exp->send("exit\n");
$exp->expect(5, ".");
# the second exit because the first one will just exit the bash and
# return to csh
$exp->send("exit\n");
The above lacks error checking as it assumes that each call to expect()
will succeed. Also, it doesn't need to make use of expect()'s more
sophisticated features such as providing a list of regular expressions
and checking its return value to figure out which one matched.
Tassilo