Cmdr_Tofu has asked for the wisdom of the Perl Monks concerning the following question:

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. :)

Replies are listed 'Best First'.
Re: Expect.pm and Perl Threading
by robartes (Priest) on Nov 24, 2002 at 14:56 UTC
    Hi,

    what happens if you replace the line:

    die "no password prompt";
    with
    die "no password prompt: $! --- $@\n";
    This way, you get more information on what the actual error was, if you're lucky.

    And a note on your idea to store passwords encrypted on disk. This is fine, as long as you make sure that i) whoever is running the script has no easy access to said file and ii) you don't put the key to decrypt the file in the script. That's like hiding your key under the doormat but taping a notice describing where to find the key to your front door.

    CU
    Robartes-

      Thank you, that did give me more information... Now I get:
      thread failed to start: no password prompt: Input/output error ---
      perhaps running stuff from a separate thread interferes with the way Expect.pm connects to the streams...

      About the encryption thing... unfortunately I have to type the decryption salt to load the plaintext passwords into my program every time :(
Re: Expect.pm and Perl Threading
by Cmdr_Tofu (Scribe) on Nov 24, 2002 at 16:08 UTC
    alas... disappointment

    I cross posted this message on the expectperl-discuss list, and Roland has told me that IO-Tty is not threadsafe (yet). So for now my code must remain ugly