From: Bryan B. <br...@bu...> - 2007-07-12 17:45:32
|
Austin/Roland, Ok, there have been a few threads lately about threading so I thought i would re-visit the issue. I wrote a script that seems to work, but I would like your opinions on if it really should be working, and/or any snafu's i may run into if I continue doing it this way. My script (which I will add to this e-mail) does the following steps in order: 1. Initialize and store 1 expect object for each host, spawning an SSH command 2. Get to a command prompt on each expect object, so we have some place to start 3. Start one thread for each host, sending a list of commands, expecting the prompt in between each command 4. Close the expect object and return, joining thread I know it works with at least 5 hosts because i keep separate logs for each expect session. I'm using Expect v1.20, IO::Tty v1.07, and i've tried this on two different hosts: SunOS 5.10 Generic_125101-09 i86pc Redhat Linux AS4 2.6.9-5.EL And finally, here is the script: --- start ---> #!/usr/bin/perl -w use strict; use threads; use Expect; $Expect::Log_Stdout = 0; our $prompt = '[>#\$] $'; my @hosts = ("ravager", "jupiter", "europa", "firebee", "invader"); my %hosts_exp; foreach my $host (@hosts) { print "Getting expect object for $host\n"; $hosts_exp{$host} = new Expect; $hosts_exp{$host}->log_file("$host.log"); $hosts_exp{$host}->spawn("ssh -x $host"); $hosts_exp{$host}->expect(5, '-re', $prompt); } my $thread_id = 0; my @thread_ids = (); my @commands = ("uptime", "who", "who am i", "pwd"); foreach my $host ( keys %hosts_exp ) { $thread_ids[$thread_id] = threads->new("run_threaded", $hosts_exp{$host}, $host, @commands); $thread_id++; } for (my $t=0; $t<$thread_id; $t++) { my $result = $thread_ids[$t]->join(); if ( ! $result ) { print "Thread $t returned bad result\n"; } } sub run_threaded { my $exp = shift; my $hostname = shift; my @commands = @_; my $result = 0; foreach my $command (@commands) { print "Executing on $hostname: $command\n"; $exp->send("$command \n"); $result = $exp->expect(5, '-re', $prompt); last if ! $result; } if ($result) { $exp->send("exit;\n exit;\n exit;\n"); $exp->soft_close(); } else { $exp->hard_close(); } return($result); } <--- end --- Thanks, Bryan Bueter http://sourceforge.net/projects/rover |