sub Connection { my ($i) = shift; # thread number my ($sem) = shift; # semaphore if ($logging==1){ # LOG on my $fileLog = "log_sess_$i.log"; my $file = open ( LOGSES , '>>',$fileLog ); } my $ssh2 = Net::SSH2->new(); $Net::SSH2::Expect::Log_Stdout = 0; if ($ssh2->connect($Host1)) { if ($ssh2->auth_password($username1,$passwd1)) { print "SSH Connection $i: established\n"; my $exp = Net::SSH2::Expect->new($ssh2); my $chan = $ssh2->channel(); my $timeout = 20; my $prompt; my $output; my $ind = 0; my $data =""; my $ppid; my @cmd = (); # command table my $numCmd; # number of commands in table my %cmdCnt = (); # command type counter my $key; # command name my $value; # counter value my $sleepTime; $sem->down(); # semaphore while ($runProvisioning==1) { $exp->expect($timeout, [ qr/ sub { for (my $id = 0; $id < 100; $id++){ # execute command table 100 times splice(@cmd); FillCmd($id,$i,\@cmd); $numCmd = @cmd; # number of commands $ind = 0; while ($ind < $numCmd){ # send all commands in table my ($exp, $prompt) = @_; COMMAND: $exp->send("$cmd[$ind]\n"); print logses "$prompt\n"; $cmd[$ind] =~ m/(.....):/; if (exists($cmdCnt{$1})){$cmdCnt{$1} += 1;} else {$cmdCnt{$1} = 1}; $ind = $ind + 1; } } }, qr/EXECUTE/ => sub { goto COMMAND; }, qr/NOT ACCEPTED/ => sub { goto COMMAND; } ]); # end expect method if($runProvisioning==0) { last; } } # end while Provisioning 1 print "\nWait while closing connection $i ...\n"; sleep (3); { lock ($runProvisioning); $runProvisioning = 0; } $sem->up(); if ($logging==1){ close(LOGSES); } print "Thread $i: closed\n"; } } }