Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Telnet Output

by jmutton (Initiate)
on Dec 30, 2013 at 16:52 UTC ( #1068760=perlquestion: print w/ replies, xml ) Need Help??
jmutton has asked for the wisdom of the Perl Monks concerning the following question:

Hello there, I'm trying to grab a couple of numbers from a telnet output of a draytek router. Here is the output:
Account:admin Password: ***** User login successful, expired time is "Unlimited". Type ? for command help > wan vdsl show basic VDSL Link Status: Showtime Firmware Version: 1414c3 VDSL Profile: 17a Basic Status Upstream Downstream Unit Actual Data Rate: 13320 39961 Kb/s SNR: 69 42 0.1dB >
The two numbers that I'm interested in are the SNR values (69 and 42). Here is my code so far. Just really stuck about parsing the values to be used in a cacti graphi.
#!/usr/bin/perl use Net::Telnet; my $telnet = Net::Telnet->new(Input_log => 'telnet.log'); $telnet->open('192.168.1.1'); $telnet->waitfor('/Account:/'); $telnet->print('admin'); $telnet->waitfor('/Password:/'); $telnet->print('admin'); sleep(1); $telnet->waitfor('/>/'); my @lines = $telnet->cmd('wan vdsl show basic'); #print @lines; $telnet->close;

Comment on Telnet Output
Select or Download Code
Re: Telnet Output
by atcroft (Monsignor) on Dec 30, 2013 at 17:05 UTC

    Here is what I would try first:

    # Here be ***UNTESTED*** code my %snr; while ( my $line = shift @lines ) { chomp $line; if ( $line =~ m/SNR:\s+(\d+)\s+(\d+)/ ) { ( $snr{up}, $snr{down} ) = ( $1, $2 ); last; } } print q{Upstream_SNR:}, $snr{up}, q{ }, q{Downstream_SNR:}, $snr{down}, qq{\n};
    (The print based on a quick glance at Chapter 11 of the Cacti manual.)

    Hope that helps.

    Update: 2013-12-30
    Fix typo in code (missing underscore in second field name).

      Thanks for the reply. How would it work if I had multiple SNRs? For examples
      SNR Margin 60 60 (US0,--) 0.1 dB SNR Margin 61 53 (US1,DS1) 0.1 dB SNR Margin 62 50 (US2,DS2) 0.1 dB SNR Margin 0 N/A (US3,DS3) 0.1 dB SNR Margin 0 N/A (US4,DS4) 0.1 dB
        my %snr; while ( my $line = shift @lines ) { chomp $line; # (US0,--) 0.1 dB if ( $line =~ m/SNR Margin:\s*(\d+)\s+(\d+)\s*\(US0,--\) 0.1 dB/ ) + { ( $snr{us0} ) = ( $1 ); } # (US1,DS1) 0.1 dB if ( $line =~ m/SNR Margin:\s*(\d+)\s+(\d+)\s*\(US1,DS1\) 0.1 dB/ +) { ( $snr{us1}, $snr{ds1} ) = ( $1, $2 ); } # (US2,DS2) 0.1 dB if ( $line =~ m/SNR Margin:\s*(\d+)\s+(\d+)\s*\(US2,DS2\) 0.1 dB/ +) { ( $snr{us2}, $snr{ds2} ) = ( $1, $2 ); } # (US3,DS3) 0.1 dB if ( $line =~ m/SNR Margin:\s*(\d+)\s+(\d+)\s*\(US3,DS3\) 0.1 dB/ +) { ( $snr{us3}, $snr{ds3} ) = ( $1, $2 ); } # (US4,DS4) 0.1 dB if ( $line =~ m/SNR Margin:\s*(\d+)\s+(\d+)\s*\(US4,DS4\) 0.1 dB/ +) { ( $snr{us4}, $snr{ds4} ) = ( $1, $2 ); } } print q{US0:}, $snr{us0}, qq{\n}; print q{US1:}, $snr{us1}, q{ }, q{DS1:}, $snr{ds1}, qq{\n}; print q{US2:}, $snr{us2}, q{ }, q{DS2:}, $snr{ds2}, qq{\n}; print q{US3:}, $snr{us3}, q{ }, q{DS3:}, $snr{ds3}, qq{\n}; print q{US4:}, $snr{us4}, q{ }, q{DS4:}, $snr{ds4}, qq{\n};
        Thats what I've managed to come up with. But I need some sort of checking as some of the SNRs will report 0 or N/A. Need it to report 0 instead of N/A.
Re: Telnet Output
by roboticus (Canon) on Dec 30, 2013 at 17:06 UTC

    jmutton:

    If you just need those SNR values, you'd first find the line(s) that have the values, such as by recognizing the 'SNR:' string:

    @lines = grep { /^SNR:/ } @lines;

    Then you could split the line on whitespace, and keep the second and third columns:

    for my $line (@lines) { my ($SNR, $val1, $val2, @rest) = split /\s+/,$line; print "SNR value 1:$val1, value 2:$val2\n"; }

    Update: Added missing '/' in regex. (Noticed by Laurent_R)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-11-26 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (164 votes), past polls