Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am having a problem with understanding expect. I have am trying to create an shh script. I have 3 different conditions:
$exp->expect($timeout, [ qr/\(yes\/no\)\?/i, sub { my $self = shift; $self->send("yes\r"); <----unk +nown host exp_continue; }], [ qr/password: /i, sub { my $self = shift; $self->send("$password\n"); <--- +--- needs password exp_continue; }], [ qr/#/i, sub { my $self = shift; <------ +-- ready to go. $self->send("ls\n"); ; }], );
So how do I, once I get through the 3 conditions continue using expect?
What I would like is to get past the three different ssh scenarios and keep going on.$exp->expect($timeout, [ qr/#/i, sub { my $self = shift; $self->send("ll\n"); <---- only + works when I have a line below (crazy that is how I know this is not + right) ; }], un +less ($exp->expect($timeout, -re , "~")){} ; );
#!/usr/bin/env perl use strict; use warnings; use Expect; #$Expect::Exp_Internal = 1; my $command = "ssh"; my $user = "root"; my @ips = ("10.16.135.157"); my $cnt= 0+@ips; my $password = "aristo1"; my $timeout = 10; for (my $i =0; $i < $cnt; $i++) { my $exp = Expect->spawn ($command, "$user"."@"."$ips[$i]"); #$exp->debug(2); $exp->expect($timeout, [ qr/\(yes\/no\)\?/i, sub { my $self = shift; $self->send("yes\r"); exp_continue; }], [ qr/password: /i, sub { my $self = shift; $self->send("$password\n"); exp_continue; }], [ qr/#/i, sub { my $self = shift; $self->send("ls\n"); ; }], ); $exp->expect($timeout, [ qr/#/i, sub { my $self = shift; $self->send("ll\n"); ; }], ); unless ($exp->expect($timeout, -re , "~")){} ; # $exp->send("time\n"); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl Expect Help
by ikegami (Patriarch) on May 21, 2025 at 21:39 UTC | |
Re: Perl Expect Help
by NERDVANA (Priest) on May 22, 2025 at 02:05 UTC | |
Re: Perl Expect Help
by tybalt89 (Monsignor) on May 24, 2025 at 21:18 UTC |