1

When using Expect in Perl to automate ssh logins, if the 1st password fails, how can we try another password?

It's not hard to find the pure Expect examples to do so but cannot find one for Expect in Perl.

use Expect;

my $bad_pwd = 'pw123';
my $good_pwd = 'pw1234';

my $exp = Expect->spawn("ssh user@example.com") or die;
$exp->expect($timeout,  [ "password: ", sub {$exp->send("$bad_pwd\n"); }]);

Thanks in advance!

pynexj
  • 19,215
  • 5
  • 38
  • 56
Steve
  • 337
  • 1
  • 4
  • 13
  • It is simpler if you use [public/private keys](https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/). Then `ssh` will not ask for the password, and there will be no chance that you type it incorrectly :) – Håkon Hægland Sep 20 '19 at 10:14
  • Yes, but the target server doesn't allow the passwordless ssh login, so I'm trying to make this Perl Expect work. – Steve Sep 20 '19 at 18:11

0 Answers0