in reply to Net::SSH2 not thread safe?

You can use also Net::OpenSSH or Net::OpenSSH::Parallel:
use Net::OpenSSH::Parallel; my $pssh = Net::OpenSSH::Parallel->new; while (my $element = <MYINPUTELEMENTS>) { if ($element =~ /(\d+)(\.\d+){3}/) { $pssh->add_host($element, user => $user, password => $password +); } } $pssh->all(cmd => { stdout_file => 'gss-%HOST%' }, 'ls -l / | wc -l') +; $pssh->all(cmd => { stdout_file => 'lcs2-%HOST%' }, 'ls -l /etc | wc - +l'); $pssh->run; my %errors = $pssh->get_errors; for my $host (keys %errors) { print "task failed for host $host: $errors{$host}\n" }

Replies are listed 'Best First'.
Re^2: Net::SSH2 not thread safe?
by Anonymous Monk on Nov 06, 2011 at 12:39 UTC

    Thanks for the answer, however parallel would slow things down considerably. I need to control the number of threads spawned, thus I would need to pass it into pssh in batches of $threads. Running parallel I would then need to wait for all of them to finish before sending a new batch. The completion time of the commands on each node can vary with factor 10 or more.

    I'll keep this in mind for a future project tho, the code is significantly easier than the threading. :-)

    -Jesper

      I need to control the number of threads spawned, thus I would need to pass it into pssh in batches of $threads.
      Net::OpenSSH::Parallel already does that for you. For instance, if you want to limit the number of SSH connections to 10, you can use:
      my $pssh = Net::OpenSSH::Parallel->new(connections => 10);