Bash – Colorful ssh banner

bashcolorsprofilessh

I would like to colorize my ssh banner. I know I can perform it like so:

In /etc/profile I can put:

echo -e "\e[1;31m Colorful text"
echo -e "\e[0m Reset"

But I have some ASCII art in the banner with special characters. Is there any way to colorize this without escaping every single special char in the ASCII art?

Best Answer

You might want to have a look at toilet. The following has been incorporated in the banner of one of the servers at my lab:

enter image description here

You can install it on Debian based systems with

sudo apt-get install toilet

TOIlet prints text using large characters made of smaller characters. It is similar in many ways to FIGlet with additional features such as Unicode handling, colour fonts, filters and various export formats.

toilet works perfectly well with ASCII art:

enter image description here


I have written a little Perl script to highlight specific regexes in text. If you use . as the regex, it will color everything a specific color:

enter image description here

The script (use -h for a tiny help message):

#!/usr/bin/env perl
use Getopt::Std;
use strict;
use Term::ANSIColor; 

my %opts;
getopts('hic:l:',\%opts);
    if ($opts{h}){
    print "Use -l to specify the letter(s) to highlight. To specify more than one patern use commas.\n -i makes the search case sensitive\n -c: comma separated list of colors\n";
    exit;
    }
my $case_sensitive=$opts{i}||undef;
my @color=("bold blue",'bold red', 'bold yellow', 'bold green', 'bold magenta', 'bold cyan', 'yellow on_magenta', 'bright_white on_red', 'bright_yellow on_red', 'white on_black');
if ($opts{c}) {
   @color=split(/,/,$opts{c});
}
my @patterns;
if($opts{l}){
     @patterns=split(/,/,$opts{l});
}
else{
    $patterns[0]='\*';
}
# Setting $| to non-zero forces a flush right away and after 
# every write or print on the currently selected output channel. 
$|=1;

while (my $line=<>) 
{ 
    for (my $c=0; $c<=$#patterns; $c++){
      if($case_sensitive){
        if($line=~/$patterns[$c]/){
        $line=~s/($patterns[$c])/color("$color[$c]").$1.color("reset")/ge; 
        }
      }
      else{
        if($line=~/$patterns[$c]/i){
          $line=~s/($patterns[$c])/color("$color[$c]").$1.color("reset")/ige; 
        }
      }
    }
    print STDOUT $line;
}
Related Question