http://www.perlmonks.org?node_id=1059324

eandalu01 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to send several commands and receive the response to the REMOTE server. It is a GSM user DB. I need to improve the rate on sending commands that I'm getting.I'm using Net::SSH::Expect module. Is there any other faster way? Also I was not able to store the response . I need it to apply counters on how many CMDs sent and so on... Many thanks in advance I've already written the following code: It is composed by 3 parts :
1) option handler 2) Subroutine Connection 3) Sub routine Cmds:
1) option handler:
print "Insert option number and press ENTER:\n"; my $userinput; $userinput = <STDIN>; 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 counte +r { 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];
2) Subroutine Connection:
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 < 10 +0; $id++){ # execute command table 100 times splice(@cmd); FillCmd($id,$i,\@cmd); $numCmd = @cmd; # number of commands $ind = 0; while ($ind < $numCmd){ # send all command +s 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"; } + } + }
3) Sub routine Cmds:
sub FillCmd { my ($step) = shift; my ($thrd) = shift; my ($refCmd) = shift; # my $comando = sprintf("AGSUI:IMSI=260000600%03d%03d,EKI=123456789 +01234567890123456789012,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; }
  • Comment on Sendind severals CMDs via SSH2 by using Net::SSH2::Expect module. Any other faster way?
  • Select or Download Code