1

I'm writing a Munin-Pluging and I like to capture the screen output from a telnet session. The output of such a session looks as follows:

...
    0x00017   0x41b3f340  BPING                       0           0         0            0         198          132            330             
    0x00018   0x41b47340  CHKFAILED                   0           0         0            0         198          132            330             
    0x00026   0x41b4f340  CIP                         0           0         0            0         370          264            634             
    0x0001e   0x41b57340  CONTROL                     0           1         0            0         3876         2178           6054            
    0x01014   0x41b5f340  UNETSRVR                    0           0         0            1         296          198            494             
    0x00037   0x41b67340  ----                        0           0         0            0         198          132            330             
    0x00000   0x43b67450  ----                        0           0         0            0         0            0              0               
    0x00000   0x4bb67450  ----                        0           0         0            0         5084         4224           9308            
    0x00000   0x49367450  ----                        0           0         0            0         14742        4158           18900           
    -------------------------------------------------------------------------------------------
                                            SUMMARY : 2           40        5            7         4898229      2728176        7626405    

This script extract the screen content into an array (@lines).

#!/usr/bin/perl

use Net::Telnet ();
use strict;
use warnings;


my $t = new Net::Telnet (Timeout => 10);

$t->port(777);
$t->open("192.168.0.1");

$t->buffer_empty;
my @lines = $t->waitfor(match =>"m/.* SUMMARY : .* \n/");

my @gagu = grep { "$_" =~ /^.*BPING.*\n/ } @lines;

print @gagu;
  • Of what type is the array @lines?
  • Why do I always get the whole content from grep and not a filtered line?
  • Is the array i got from net:telnet different from other arrays?

Yes, I'm new to Perl.

Maus
  • 2,724
  • 5
  • 32
  • 37
  • No arrays are different in perl, they just contain different things. If you want to see what is in your data, use the [`Data::Dumper`](http://search.cpan.org/perldoc?Data%3A%3ADumper) module. – TLP Dec 03 '12 at 16:00
  • 1
    I tested your script with the provided input from a txt file. It works as you expect. Please check your lines-array with Data::Dumper. – Demnogonis Dec 03 '12 at 16:01

1 Answers1

1

I am not familiar with this module and what it does, but I assume it gives you some sort of return value similar to what you have stated.

If you are getting all the lines in your @gagu array, that can be either that your data in the @lines array consists of just one line, or that the grep fails.

For example, @lines may contain the string:

"foo   bar    baz\nfoo1   bar1   baz1\n";

and not, as you expect

"foo   bar    baz\n";
"foo1  bar1   baz1\n";

Your grep statement probably works as expected, though you might want to consider:

  • Not quoting $_, since that serves no purpose.
  • Not using $_ at all, since that is the default variable it is not needed (except for clarity) to use it.
  • Not using anchors ^ and \n, because they are redundant.

For example, ^.* matches any string, anywhere. Using it to simply match a string is redundant. Ending the regex with .*\n is redundant, because all it says is "match any character except newline until we find a newline". Assuming you have newlines, it does nothing. Assuming you don't, it gives you a false negative. All you need for this match is /BPING/. So here's what your code might look like:

use Data::Dumper;
my @lines = $t->waitfor(match =>"m/ SUMMARY :/");
my @gagu = grep /BPING/, @lines;

print Dumper \@gagu;

If you want to see whitespace printed out visibly, you can use the $Data::Dumper::Useqq variable:

$Data::Dumper::Useqq = 1;
print Dumper \@gagu;

Printing variables is a very good debugging tool.

TLP
  • 66,756
  • 10
  • 92
  • 149