madhurikl has asked for the wisdom of the Perl Monks concerning the following question:
I am using Net::SSH2 module to make ssh connections to remote machines. I am trying to invoke these ssh connections as threads, and am getting the below errors :
Here's the code sample that I am using :Attempt to free non-existent shared string 'io_socket_type', Perl inte +rpreter: 0x84972e0 during global destruction. Attempt to free non-existent shared string 'io_socket_proto', Perl int +erpreter: 0x84972e0 during global destruction. Attempt to free non-existent shared string 'io_socket_domain', Perl in +terpreter: 0x84972e0 during global destruction. Attempt to free non-existent shared string 'io_socket_timeout', Perl i +nterpreter: 0x84972e0 during global destruction.
Package code:
package sshConnector; sub new { my $class = shift; my $deviceInfo = shift; my $self = {}; my $ssh2; my ($ip,$port,$username,$password) = @$deviceInfo; # Invoke an ssh connection to the device eval { $ssh2 = Net::SSH2->new(); $ssh2->connect($ip,$port) or die }; if ($@) { print "Failed to make connection $@\n"; return 1; } # Send credentials for authentication my $res; if ($res = $ssh2->auth_keyboard($username, $password)) { $self->{'HANDLE'} = $ssh2; } return $self; } sub cmd { my $self = shift; my $cmd = shift; my $bufLength = 10000; my $connHandle = $sshHandle->channel(); # Make the connection blocking - wait until cmd exits $connHandle->blocking(1); # Merge the stderr contents also into the stdout buffer $connHandle->ext_data('merge'); # Execute the command eval { $connHandle->exec($cmd); }; if ($@) { print "Failed to execute cmd\n"; return 1; } # Initialize the buffer with all 0s $buf = '0' x $bufLength; # Read the buffer if anything is present in the output $connHandle->read($buf,$bufLength)) { $output = $buf; $exitStatus = $connHandle->exit_status(); print "output is $output, exit status is $exitStatus\n"; $connHandle->exec('exit'); $connHandle->close; return ($exitStatus,$output); } 1;
Script:
use sshConnector; my $machine1 = new Connector($deviceInfo1); my $machine2 = new Connector($deviceInfo2); sub methodA { my $self = shift; my $count = shift; my $hostname; while ($count) { ($status,$hostname) = $self->cmd("hostname"); print "$count : $hostname\n"; sleep 1; $count--; } } sub methodB { my $self = shift; my $count = shift; my $hostname; while ($count) { ($status,$hostname) = $self->cmd("hostname"); print "$count : $hostname\n"; sleep 2; $count--; } } my $thr1 = threads->create('methodA',$machine1,10); my $thr2 = threads->create('methodB',$machine2,6); $thr1->join(); print "First thread over\n"; $thr2->join(); print "Second thread over\n";
The threads started and continued till end, but the script exits before printing the last two print statements. Can someone please help? Why am I getting this error? Net-SSH2 is a threadsafe module, as far as I know. Am I missing something?
Regards, Madhuri
|
|---|