K
K
Konkase2014-07-18 14:45:21
Perl
Konkase, 2014-07-18 14:45:21

Why sometimes broken data comes through Net::Telnet in perl ?

There is a script. Opens a session with the server, sends commands, receives the output of these commands in response, parses something and gets the number of lines.
perl ./test.pl all 192.168.0.1 23 001,002,003
the composition of the command depends on the number of the last argument, in which the data is separated by a comma, each block before the comma is a separate command. in this case, 3 telnet commands will be sent to the server.

stat #001001000
stat #001002000
stat #001003000

The fact is that the answer from the execution of these commands does not always come in its entirety. sometimes only half of the output from any of the commands comes. I can not understand the reason for such a mess :) Please, tell me how to debug this?
I watched the pearl log, the commands are sent in full, but sometimes it receives only half, but not always. About 2 times out of 10 broken answers come. There is no loss to the server. if implemented using bash and netcat, then 100% correct answers always come. But this option is not suitable, Net::Perl is needed.
The script itself:
#!/usr/bin/perl
use Net::Telnet;
use Switch;

$type = $ARGV[0];
$ipats = $ARGV[1];
$port = $ARGV[2];
$stream = $ARGV[3];
@streams = split(',', $stream);

switch($type){
  case "one" 			{	$reg="one";					}
  case "two" 			{	$reg="two";					}
  case "three" 		{	$reg="three";				}
  case "other" 		{	$reg="one|two|three";		}
  case "all" 			{	$reg=".*";					}	
}

$swi = new Net::Telnet (Host => $ipats ,Port => $port , Timeout => 20, Prompt => '/>$/'); 
foreach $str (@streams) {
  chomp $str;
  @output = $swi->cmd(String   => 'stat #001'.$str.'000');	 
  foreach $resultline (@output) {
      if ($resultline =~ m/state=\"($reg)\"/ )	{
        push @arr, $1;
        #chomp $resultline;print $resultline."	".$str."\n";
      }

  } 
}
print "". @arr."\n";
#print scalar(@arr);
$swi->close();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadz Goldman, 2015-08-09
@nadz

Net::Telnet needs a bit of patching. I won’t give a link, because I’m greedy (but Google doesn’t).
Another option is not to patch, but to set waitfor more, prompt is always valid and correct.
As a means - in the block where we accept, we specify waitfor additionally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question