|
From: Alex H. <ale...@gm...> - 2011-07-10 12:26:21
|
Hi list,
I am trying to write my first Expect Perl script and have run into a wall
and wonder if someone can help.
The script is supposed to connect to the console of a Solaris firewall,
enter the existing password, change the password to a new one, and then
logout and disconnect.
Here is the code, which is based on the example from the CPAN home page:
#!/usr/bin/perl
use warnings;
use strict;
use Expect;
my $exp = Expect->spawn("./emulate_console.sh")
or die "Cannot spawn command: $!\n";
my $spawn_ok;
my $newpass = 'YYYYYYYY';
$exp->expect(
10,
[
qr/for help.*$/ => sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("\r");
exp_continue;
}
],
[
qr/login: $/ => sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("root\r");
exp_continue;
}
],
[
qr/Password: $/ => sub {
$spawn_ok = 1;
my $fh = shift;
$fh->send("XXXXXXXX\r"); # send the current password
exp_continue;
}
],
[
eof => sub {
if ($spawn_ok) {
die "ERROR: premature EOF in login.\n";
} else {
die "ERROR: could not spawn console.\n";
}
}
],
[
timeout => sub {
die "No login.\n";
}
],
'-re', qr/# $/, # wait for shell prompt, then exit expect
);
$exp->send("passwd root\r");
$exp->expect(
10,
[
qr/New Password: $/ => sub {
my $fh = shift;
$fh->send("$newpass\r");
exp_continue;
}
],
[
qr/Re-enter new password: $/ => sub {
my $fh = shift;
$fh->send("$newpass\r");
exp_continue;
}
],
);
$exp->send("\cD"); # logout
$exp->send("\cEc."); # key sequence to disconnect from console
Now emulate_console.sh is just a simple shell script that pretends to be the
server and prints the same text to the screen as the server's console would.
This shell script is not the issue, as I get more or less the same result
using the real server.
The result I get is this:
It logs in fine, and issues the 'passwd root' command. It sends the new
password and then sends it a second time at the 'Re-enter new password'
prompt. Then it just mysteriously stops. When using the emulate_terminal.sh
script, that script never sends any further output to the screen once the
second password is sent (which means I never see the root prompt that should
follow this). I get more or less the same result when I use the real server.
I am pulling my hair out now so any hints would be most appreciated. Also
some example code would be much appreciated (as can probably be seen I
really don't know what I am doing. :)
Best regards,
Alex Harvey
|