#!/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$_"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: fork + Expect.pm + ssh == success!
by Juerd (Abbot) on Dec 26, 2002 at 11:40 UTC | |
by Cmdr_Tofu (Scribe) on Dec 29, 2002 at 00:13 UTC | |
by Juerd (Abbot) on Dec 29, 2002 at 00:27 UTC |