?
Solved

Separate show dsl command in perl for use in cacti

Posted on 2010-01-13
3
Medium Priority
?
465 Views
Last Modified: 2013-12-26
Need a way using regex to separate an array into separate variables.

Just need to output the numeric values for speed/attenuation/noise_margin

I tried to do this with "split" but it doesn't work.  I would rather do it with regex but my programming skills are limited.  I know cisco can use snmp for cacti graphs, but i evenutually want to modify this script for different routers.

If someone could provide the code i need to separate out the following output into separate varialbles and expalin the regex so i can change it if necessary:

ATM0
Alcatel 20190 chipset information
                ATU-R (DS)                      ATU-C (US)
Modem Status:    Showtime (DMTDSL_SHOWTIME)
DSL Mode:        ITU G.992.1 (G.DMT) Annex A
ITU STD NUM:     0x01                            0x1
Vendor ID:       'STMI'                          'GSPN'
Vendor Specific: 0x0000                          0x0008
Vendor Country:  0x0F                            0xFF
Chip ID:         C196 (0)
DFE BOM:         DFE3.0 Annex A (1)
Capacity Used:   93%                             100%
Noise Margin:    12.5 dB                         13.0 dB
Output Power:    13.0 dBm                        12.5 dBm
Attenuation:      9.0 dB                          3.0 dB
FEC ES Errors:    0                               0
ES Errors:       21216                           1348
SES Errors:       0                               0
LOSES Errors:     0                               0
UES Errors:       0                               0
Defect Status:   None                            None
Last Fail Code:  None
Watchdog Counter: 0xA3
Watchdog Resets: 0
Selftest Result: 0x00
Subfunction:     0x00
Interrupts:      4191 (0 spurious)
PHY Access Err:  0
Activations:     3
LED Status:      ON
LED On Time:     100
LED Off Time:    100
Init FW:         init_AMR-3.0.014_no_bist.bin
Operation FW:    AMR-3.0.014.bin
FW Source:       embedded
FW Version:      3.0.14

                 Interleave             Fast    Interleave              Fast
Speed (kbps):          8160                0           704                 0
Cells:             87842588                0    1254180885                 0
Reed-Solomon EC:          7                0             2                 0
CRC Errors:               0                0             3                 0
Header Errors:            0                0             0                 0
Total BER:                0E-0           0E-0
Leakage Average BER:      0E-0           0E-0
                        ATU-R (DS)      ATU-C (US)
Bitswap:               enabled            enabled
Bitswap success:          0                   0
Bitswap failure:          0                   0

LOM Monitoring : Disabled


DMT Bits Per Bin
000: 0 0 0 0 0 0 2 4 6 7 8 8 9 A A A
010: A A A A A A A A 9 9 9 8 8 7 7 6
020: 0 0 0 9 A A B B B B B B C C B C
030: C C C C C C B B B B B B B B B B
040: 0 B B B B B B B B B B B B B B B
050: B B B B B B B B B B B B B B B B
060: B B B B 2 B B B B B B B A B B B
070: A A A A A A A A A A A A A A A A
080: A A A A A A A A A A A A A A A A
090: A A A A A A A A A A A A A A A A
0A0: A A A A A A A A A A A A A A A A
0B0: A A A A A A A A A A A A A A A A
0C0: A A A A A A A A A A A A A A A A
0D0: A B A A A A A B A A A A A A A A
0E0: A A A A A A A A A A A A A A 9 9
0F0: A 9 A A 9 9 9 9 9 9 9 9 9 9 9 9

DSL: Training log buffer capability is not enabled
#!/usr/bin/perl

use Net::Telnet::Cisco;
use strict;

my $host = "xxxxxxx";
my $username = "xxxxxxxx";
my $password = "xxxxxxxxx";

my $session = Net::Telnet::Cisco->new(Host => $host, Timeout=> '120', Errmode=> 'return');

unless($session) {
            print "Could not connect to device: $host\n";
      next;
}

$session->login($username, $password);

if($session->errmsg) {
print "Authentication failed";
      }

#my @output = "";
#@output = $session->cmd('sh dsl int ATM0');
my @status = $session->cmd('sh dsl int atm0');
$session->close;

chomp(@status);
my $split = "@status";
my @split = split(/ /, $split);

@split = grep /\S/, @split;
open FILE, ">/home/rbignell/adsl_status.txt" or die $!;
   for my $char (@split) {
        print FILE $char . "\n";
   }
close FILE;

print "downstream_speed:$split[146] ";
print "upstream_speed:$split[148] ";
print "downstream_attenuation:$split[63] ";
print "upstream_attenuation:$split[65] ";
print "downstream_noisemargin:$split[52]";
print "upstream_noisemargin:$split[55]";

Open in new window

0
Comment
Question by:bignellrp
3 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 26305725
If all of the output is in a variable, you can get a particular lines value with something like:
    #assuming $str contains all of the output
    my ($NoiseMargin) = $str =~ /Noise Margin:\s+(.*)/;
0
 
LVL 27

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 26305899
This modified version of your code should work (I don't have a Cisco to test on).
#!/usr/bin/perl

use Net::Telnet::Cisco;
use strict;

my $host = "xxxxxxx";
my $username = "xxxxxxxx";
my $password = "xxxxxxxxx";

my $session = Net::Telnet::Cisco->new(Host => $host, Timeout=> '120', Errmode=> 'return');

unless($session) {
    print "Could not connect to device: $host\n";
    next;
}

$session->login($username, $password);

if($session->errmsg) {
    print "Authentication failed";
}

my @status = $session->cmd('sh dsl int atm0');
$session->close;

# put the status into a string (including newlines)
my $stat = join '', @status;

# regex
if ($stat =~ m{
        # look for Noise Margin label and extract numerics after
        # (do not include dB)
        \s+Noise\s+Margin:\s+([\d\.]+)\s+dB\s+([\d\.]+)\s+dB\s+.*
        # same for Attenuation
        \s+Attenuation:\s+([\d\.]+)\s+dB\s+([\d\.]+)\s+dB\s+.*
        # look for Speed label and extract every other numeric
        \s+Speed\s+\(kbps\):\s+(\d+)\s+\d+\s+(\d+)\s+\d+\s+
        }xs) {

    print "downstream_noisemargin:$1 ",
          "upstream_noisemargin:$2 ",
          "downstream_attenuation:$3 ",
          "upstream_attenuation:$4 ",
          "downstream_speed:$5 ",
          "upstream_speed:$6\n";
} else {
    print "regex did not match\n";
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:bignellrp
ID: 31676595
thx...works perfectly
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Six Sigma Control Plans
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question