Fellow monks, for many months have I struggled with a single goal: to simultaneously control multiple ssh sessions from a perl script. As I discovered, using Expect.pm in series becomes painfully with many machines, and Perl Threading does not seem to be compatible with IO-Tty.

However, at long last an answer is to be had. A code skeleton follows (for demonstration purposes only):
#!/usr/local/perl-5.8.0/bin/perl use strict; use threads; use Expect; ####### # # Here is the section that we want to run many times in parallel # ####### 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(); } ###### # # Here is the replacement for: # my $h = threads->new(\&mysub); # $h->join; # ###### my $PARENTPROCESS=$$; my @streamlist; my @childlist; for (every machine); my $KID; my $child = open($KID,"-|"); if ($$==$PARENTPROCESS) { push @streamlist, $KID; push @childlist, $child; next; } else { $| = 1; printf("begin--running forked ssh session on $thismachine--\n"); mysub; printf("end--running forked ssh session on $thismachine--\n"); exit(); } } #let child processes terminate my $kid; do { $kid = wait(); } until $kid == -1; #print all the output foreach my $stream (@streamlist) { while (<$stream>) { print "\t$_"; } }

I appreciate any comments / criticisms on this code. I will likely be using it in production tomorrow. I made a module that I will be using (available on request) to update a bunch of systems. All passwords are stored in encrypted file which are decrypted at runtime.

Rohit

In reply to fork + Expect.pm + ssh == success! 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.