|
From: Bob M. <ram...@gm...> - 2017-03-24 21:52:15
|
Hello List,
I'm supporting legacy code that uses Expect, but I'm no expert with this
tool. Nor can I figure out a way to pose the question to the archive
server to search for anything helpful, so here I am, hoping for an answer.
:)
Before the section that does the Expect processing, there is a sub defined
as follows:
$passHandler = sub {
my ($ssh, $logging, $password) = @_;
$ssh->log_file(undef);
$ssh->stty(qw(-echo));
print $ssh "$password\r";
$ssh->stty(qw(echo));
$pat = quotemeta($password);
$ssh->expect(0, '-re', "$pat");
$ssh->log_file(\&logMethod) if $logging;
return $ssh->error();
};
There are a number of other patterns to look for besides the two prompts
listed below, each with appropriate logic contained in other 'sub'
definitions. My problem is with the second prompt. Also, I'm not being
pedantic here about format or syntax, this is not intended to run
(currently).
$expectObj->expect($timeout,
'-re', $prompt1, sub {
$failed = $passHandler->($ssh, $logging, $password);
if ($failed) {
return "something";
} else {
return exp_continue_timeout();
}
},
'-re', $prompt2, sub {
$output = $ssh->exp_before();
if ($secondPass) {
$failed = $passHandler->($ssh, $logging, $secondPass);
if ($failed) {
return 'something';
} else {
return exp_continue_timeout();
}
} else {
return # an error
}
},
...
);
I have run the original code with debugging on.
Processing $prompt1 happens before processing for $prompt2. $prompt1 is
the login prompt passed through by 'ssh'.
Handling the ssh login step works fine.
Processing the second prompt looks for a string like 'Enter secondary
password: ' which is detected correctly and processing goes into the
passHandler, where the password is sent.
The function then does the 'expect' on the password "pattern", which is
there to be sure we don't get any echo'ed password passed through.
But in the failing case, there is no password echoed, and the next set of
returned strings contains stuff that doesn't match, within the
passHandler. So it times out.
Upon returning to the enclosing 'expect' call, expect immediately begins
'waiting' for new data. The strings that were picked up in passHandler are
not looked at, and unfortunately they have the next string being 'waited'
for, so the whole thing times out and processing fails.
I'm happy to try to make something that actually runs, if that is needed,
but that will take a bit more time and I hope this is enough to point
someone in the right direction.
Many thanks,
Bob
|