App-RouterColorizer

 view release on metacpan or  search on metacpan

bin/router-colorizer.pl  view on Meta::CPAN

#!/usr/bin/env perl

#
# Copyright (C) 2021-2023 Joelle Maslak
# All Rights Reserved - See License
#

# PODNAME: router-colorizer.pl
# ABSTRACT: Colorize router CLI output

use v5.22;
use strict;
use warnings;

use feature 'signatures';
no warnings 'experimental::signatures';

use App::RouterColorizer;

my $TIMER = 0.01;    # Delay to wait for more data, in secs.

MAIN: {
    my $colorizer = App::RouterColorizer->new();

    STDOUT->autoflush(1);
    STDIN->blocking(0);

    my $buffer = '';
    my $rin    = '';
    vec( $rin, fileno(STDIN), 1 ) = 1;
    while (1) {
        select( my $rout = $rin, undef, my $eout = $rin, $TIMER );
        if ( vec( $rout, fileno(STDIN), 1 ) || vec( $eout, fileno(STDIN), 1 ) ) {
            # We have characters to potentially read
            my $tmp = '';
            read STDIN, $tmp, 65535, 0;
            if ( length($tmp) ) {
                $buffer .= $tmp;

                # We want to process complete lines only, without a
                # timeout.
                my ( $process, $newbuf ) = $buffer =~ m/^ (.*) (\n .*) $/sxx;
                $buffer = $newbuf // $buffer;

                if ( defined($process) ) {
                    print $colorizer->format_text($process);
                } elsif ( length($buffer) == 1 ) {
                    # One exception to processing complete lines is
                    # single character buffers - for these, we want to
                    # output the single character quickly to not cause
                    # lag in interactive sessions.
                    print $colorizer->format_text($buffer);
                    $buffer = '';
                }
            } else {
                # End of file
                print $colorizer->format_text($buffer) unless $buffer eq '';
                exit;
            }
        } else {
            # Timeout!
            if ( $buffer ne "" ) {
                print $colorizer->format_text($buffer);
                $buffer = "";
            }
        }
    }
}

__END__

=pod

=encoding UTF-8

=head1 NAME

router-colorizer.pl - Colorize router CLI output

=head1 VERSION

version 1.233180

=head1 DESCRIPTION

This script colorizes the output of router output, using
the L<App::RouterColorizer> module.  This script takes no arguments.

The output will be colorized based on detection of key strings as they
might be sent from Arista, Juniper, and VyOS routers.  It may also work
on other router outputs, but these have not been used for development.

  /usr/bin/ssh router.example.com | router-colorizer.pl

=head1 COLOR CODING

Color coding is used, which assumes a black background on your terminal.
The colors used indicate different kinds of output. Note that most lines
of output are not colorized, only "important" (as defined by me!) lines
are colorized.

=over 4

=item C<green>

Green text is used to signify "good" values. For instance, in the output
from C<show interfaces> on an Arista router, seeing lines indicating the
circuit is "up" and not showing errors will show in green.

=item C<orange>

Orange text is used to show things that aren't "good" but also aren't "bad."
For instance, administratively down interfaces in C<show interfaces status>
will typically show as orange.

=item C<red>

Red text indicates error conditions, such as errors being seen on the
output of C<show interfaces>.

=item C<cyan>

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.478 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )