Hello, everyone. Yet again I am in the need of sagely monk wisdom to solve my problems...

I am trying to clean up my ugly Expect.pm code making my own module and taking advantage of the new (ly stable) threading support in perl. Does anyone know if Expect.pm is a thread-safe module? If it is I must be doing something wrong, because I can't get the basics working yet.

Perhaps I am making an obvious error. I will describe what I have done - The following code works fine - no problems:
#!/usr/local/perl-5.8.0/bin/perl use strict; use threads; use Expect; sub mysub { my $remoteshell; unless ( $remoteshell = Expect -> spawn ("ssh2 192.168.1.1") ) { die "error spawning"; } $remoteshell -> log_stdout(1); unless ( $remoteshell -> expect (120, [ "ssword:" => sub { $remoteshell -> send ("mypasswo +rd\n"); } + ], ) ) { die "no password prompt"; } unless ( $remoteshell -> expect (20,"ro\@charon:") ) { die "no prompt"; } print $remoteshell "touch /tmp/hopo\n"; $remoteshell ->soft_close(); } &mysub;
However, if I do the same thing in a seperate thread, by replacing the call to mysub with the following:
my $h = threads->new(\&mysub); $h->join;
The program dies instantly with the error:
thread failed to start: no password prompt at ./expect.pl line 21.
From the processlist I can see that an ssh2 is launched, but the calling program does not even wait the 120 second timeout before dieing with the above error.

If anyone can offer any advice to solve this I would be most grateful.

Thanks!

Rohit (aspiring perl-guru)

PS: Please note I am NOT storing my passwords in the perl script. That is a stripped down example. The passwords go in an openssl encrypted text-file which is never stored to disk in its unencrypted form. :)

In reply to Expect.pm and Perl Threading by Cmdr_Tofu

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.