Hi All

I am using perl expect module in our legacy application to scp some file to othe machine. The password for the client machine changes every 15 days, so we can't use ssh public keys.

For most of the machine the Expect script as expected, but for some the the prompt of the machine appears after the perl expect part code is excecuted.

Here is the code of the script.

$Expect::Exp_Internal = 1; my $exp = Expect->new(); #$exp->log_stdout(1); $exp->spawn("scp test\.txt foo\@bar:/tmp/test\.txt ") or warn("Unable + to spwan : $!"); $exp->expect(9, [ qr/Are you sure you want to continue connecting \(yes\/ +no\)\?/i, sub { my $self = shift; $self->send("yes\n"); exp_continue; }], [ qr/password:/i, sub { my $self = shift; $self->send("$passwd\n"); exp_continue; }]) or warn("error : " .$exp-> +exp_error());

Here is the output of the script.

Spawned 'scp test.txt foo@bar:/tmp/test.txt ' spawn id(6) Pid: 17288 Tty: /dev/pts/1 Expect::spawn('Expect=GLOB(0x87a04a8)','scp test.txt foo@bar: +/tmp/test.txt co...') called at ... main::foo_project('ARRAY(0x8166e00)') called at ..... Starting EXPECT pattern matching... Expect::expect('Expect=GLOB(0x87a04a8)',9,'ARRAY(0x89e0d04)',' +ARRAY(0x89e8748)') called at ... main::foo_project_project('ARRAY(0x8166e00)') called at .... spawn id(6): list of patterns: #1: -re `(?i-xsm:Are you sure you want to continue connecting \(yes/ +no\)\?)' #2: -re `(?i-xsm:password:)' spawn id(6): Does `' match: pattern #1: -re `(?i-xsm:Are you sure you want to continue connectin +g \(yes/no\)\?)'? No. pattern #2: -re `(?i-xsm:password:)'? No. Waiting for new data (9 seconds)... TIMEOUT Returning from expect with TIMEOUT or EOF error : 1:TIMEOUT at .... foo@bar's password:

Any help in this regard will be appreciated or is there any other way to achieve the same?

stackoverflow link


In reply to perl expect script ending prematurely before supplying the password by madeinindia

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.