in reply to Net::SSH2 not thread safe?
Before you move to using processes, you could try this (untested) alternative formulation and see what happens:
#!/usr/bin/perl -w use strict; use threads; use Thread::Queue; my $Q = new Thread::Queue; my @pool = map threads->create( \&worker_parse, $Q ), 1 .. 10; open (MYINPUTELEMENTS, "./list.txt"); while( my $element = <MYINPUTELEMENTS> ) { if( $element =~ /(\d+)(\.\d+){3}/ ) { $Q->enqueue( $element ); } } close (MYINPUTELEMENTS); $Q->enqueue( (undef) x 10 ); $_->join for @pool; sub worker_parse { my $Q = shift; require 'Net::SSH2'; while( my $inIP = $Q->dequeue ) { chomp( $inIP ); my $ssh2 = Net::SSH2->new(); if ($ssh2->connect($inIP)) { if ($ssh2->auth_password('user','pass')) { my $gssChannel = $ssh2->channel(); $gssChannel->exec('ls -l / | wc -l'); my $gssData; my $gssLen = $gssChannel->read($gssData,8192); chomp($gssData); print "gss: $gssData - "; $gssChannel->close; my $lcsChannel = $ssh2->channel(); $lcsChannel->exec('ls -l /etc | wc -l'); my $lcs2Data; my $lcs2Len = $lcsChannel->read($lcs2Data,8192); chomp($lcs2Data); print "lcs2: $lcs2Data\n"; $lcsChannel->close; $ssh2->disconnect; } else { print "Authentication Failed\n"; } } else { print "Connection Failed\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Net::SSH2 not thread safe?
by Anonymous Monk on Nov 06, 2011 at 12:42 UTC | |
by BrowserUk (Patriarch) on Nov 06, 2011 at 13:35 UTC |