I am having a problem with understanding expect. I have am trying to create an shh script. I have 3 different conditions:

  1. host is unknown it needs to be added to known host list,
  2. host needs password,
  3. host dose not need password as it is using ssh key.

$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?

$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 , "~")){} ; );
What I would like is to get past the three different ssh scenarios and keep going on.

#!/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"); }

In reply to Perl Expect Help by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.