1) option handler
2) Subroutine Connection
3) Sub routine Cmds:
####
print "Insert option number and press ENTER:\n";
my $userinput;
$userinput = ;
chomp ($userinput);
print "Option choosen $userinput, press ENTER\n";
ReadMode 1;
#ReadMode('noecho');
$option= ReadKey(6);
$option=$userinput;
if($option==1){
if($runProvisioning==1){
print "\nError!\nProvisioning already started\n";
$option = 0;
sleep(3);
next;
}
splice(@thr); # clear thread table
%cmdTotal = (); # clear command counters
$cmdFailed = 0; # clear fail counter
{
lock ($runProvisioning);
$runProvisioning = 1;
}
my $semaphore = Thread::Semaphore->new($num_con);
$semaphore->down($num_con); # create semaphore = 0
print "\nWait...\n";
# create threads
for(my $i = 0; $i < $num_con; $i++) {
push @thr, threads->new(\&Connection, $i,$semaphore);
sleep(2);
}
# foreach (@thr) {
# $_->join();
# }
sleep($wait2start);
print "Provisioning started...\n";
sleep (2);
$semaphore->up($num_con);
yield();
$startTime = [gettimeofday];
####
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";
}
}
}
####
sub FillCmd {
my ($step) = shift;
my ($thrd) = shift;
my ($refCmd) = shift;
# my $comando = sprintf("AGSUI:IMSI=260000600%03d%03d,EKI=12345678901234567890123456789012,KIND=325,A3A8IND=4,A4IND=2;", $thrd, $step);
# push @$refCmd, $comando;
my $comando = sprintf("HGSUI:IMSI=260000600%03d%03d,MSISDN=49600%03d%03d,PROFILE=3;", $thrd, $step, $thrd, $step);
push @$refCmd, $comando;
$comando = sprintf ("HGSUE:MSISDN=49600%03d%03d;", $thrd, $step, $thrd, $step);
push @$refCmd, $comando;
}