use Net::SSH::Expect;
$curr_pwd = 'test';
$new_pwd = 'blablablabla';
my $ssh = Net::SSH::Expect->new (
host => "localhost",
password=> $curr_pwd,
user => 'test',
raw_pty => 1
);
my $login_output = $ssh->login();
if ($login_output !~ /Last login/) {
die "Login has failed. Login output was $login_output";
}
# my $ls = $ssh->exec("ls -l /");
# print($ls);
$ssh->send("passwd");
$ssh->waitfor('password:\s*\z', 1) or die "prompt 'password' not fou
nd after 1 second";
$ssh->send($curr_pwd);
$ssh->waitfor(':\s*', 1) or die "prompt 'New password:' not found";
$ssh->send($new_pwd);
$ssh->waitfor(':\s*', 1) or die "prompt 'Confirm new password:' not found";
$ssh->send($new_pwd);
# check that we have the system prompt again.
my ($before_match, $match) = $ssh->waitfor('>\s*', 1); # waitfor() in a list context
die "passwd failed. passwd said '$before_match'." unless ($match);
# closes the ssh connection
$ssh->close();