Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Sendind severals CMDs via SSH2 by using Net::SSH2::Expect module. Any other faster way?

by eandalu01 (Initiate)
on Oct 23, 2013 at 16:26 UTC ( #1059324=perlquestion: print w/ replies, xml ) Need Help??
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1059324]
Approved by Laurent_R
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2015-07-02 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls