Utilizing color in ssh session (example with Android)

When using ssh to connect to “small” devices like android or raspberry, one might not have an option to install several packages. In those scenarios one can reuse the existing command line tools that are often included.

This tip is showing how to use awk to use color when looking through log files or printouts. No additional installation is needed, since the awk scripts can be executed directly from command line. The colors are created by awk script which is doing it by generating ANSI escape codes. The terminal software then interprets these codes and shows the colors.

In case of android, tool called awk is included in busybox or sshdroid installation.

What is needed:

  • awk in android (included in sshdroid)
  • ssh connection (using putty)

Coloring words, example with command: mount

android-awk-mount

The command, including the awk script, is shown below. Awk is used to highlight words with colors.

mount | awk ‘BEGIN {IGNORECASE=1;
black =sprintf(“\033[1;30;40m&\033[0m”);
red =sprintf(“\033[1;31;40m&\033[0m”);
green =sprintf(“\033[1;32;40m&\033[0m”);
yellow =sprintf(“\033[1;33;40m&\033[0m”);
blue =sprintf(“\033[1;34;40m&\033[0m”);
purple =sprintf(“\033[1;35;40m&\033[0m”);
cyan =sprintf(“\033[1;36;40m&\033[0m”);
white =sprintf(“\033[1;37;40m&\033[0m”);};
{gsub( $1,blue ,$1);
gsub( $2,yellow ,$2);
gsub( $3,cyan ,$3);
gsub(“rw,”,red ,$0);
gsub(“ro,”,green ,$0);
print}’

Coloring lines, example with command: logcat

android-awk-logcat

The command, including awk script is shown below. Awk is used to highlight full lines.

logcat | awk ‘BEGIN {IGNORECASE=1};
function black(string) { print “\033[1;30m” string “\033[0m “; }
function red(string) { print “\033[1;31m” string “\033[0m “; }
function green(string) { print “\033[1;32m” string “\033[0m “; }
function yellow(string) { print “\033[1;33m” string “\033[0m “; }
function blue(string) { print “\033[1;34m” string “\033[0m “; }
function purple(string) { print “\033[1;35m” string “\033[0m “; }
function cyan(string) { print “\033[1;36m” string “\033[0m “; }
function white(string) { print “\033[1;37m” string “\033[0m “;
{if ($0 ~ /location/) red($0);
else if ($0 ~ /LocSvc/) red($0);
else if ($0 ~ /wifi/) yellow($0);
else if ($0 ~ /WLAN/) yellow($0);
else if ($0 ~ /connectivity/) yellow($0);
else if ($0 ~ /google/) blue($0);
else if ($0 ~ /browser/) blue($0);
else if ($0 ~ /root/) cyan($0);
else if ($0 ~ /supersu/) cyan($0);
else if ($0 ~ /busybox/) cyan($0);
else print $0}’

Coloring words, example with command: logcat

android-awk-logcat-words

And the code:

logcat | awk ‘BEGIN {
IGNORECASE=1;
red =sprintf(“\033[1;31;40m&%c\033[0m”, 0x1B);
yellow =sprintf(“\033[1;33;40m&%c\033[0m”, 0x1B);
blue =sprintf(“\033[1;34;40m&%c\033[0m”, 0x1B);};
{gsub(“location”,red);
gsub(“wifi” ,yellow);
gsub(“google” ,blue);
print}’

Coloring words/columns, example with command: top

android-awk-top

And the code:

top -b | head -30 | awk ‘BEGIN
{IGNORECASE=1;
black =sprintf(“\033[1;30;40m&\033[0m”);
red =sprintf(“\033[1;31;40m&\033[0m”);
green =sprintf(“\033[1;32;40m&\033[0m”);
yellow =sprintf(“\033[1;33;40m&\033[0m”);
blue =sprintf(“\033[1;34;40m&\033[0m”);
purple =sprintf(“\033[1;35;40m&\033[0m”);
cyan =sprintf(“\033[1;36;40m&\033[0m”);
white =sprintf(“\033[1;37;40m&\033[0m”);};
{gsub(” radio “,yellow,$0);
gsub(” system “,blue,$0);
gsub(” root “,red,$0);print}’

Coloring words/columns, example with command: ps

android-awk-ps

And the code:

ps -w | awk ‘BEGIN
{IGNORECASE=1;
black =sprintf(“\033[1;30;40m&\033[0m”);
red =sprintf(“\033[1;31;40m&\033[0m”);
green =sprintf(“\033[1;32;40m&\033[0m”);
yellow =sprintf(“\033[1;33;40m&\033[0m”);
blue =sprintf(“\033[1;34;40m&\033[0m”);
purple =sprintf(“\033[1;35;40m&\033[0m”);
cyan =sprintf(“\033[1;36;40m&\033[0m”);
white =sprintf(“\033[1;37;40m&\033[0m”);};
{gsub(” radio “,yellow,$0);
gsub(” system “,blue,$0);
gsub(” root “,red,$0);print}’

Some explanations

  • in awk, $0 means full line, $1 is the first field in the line, $2 is the second field etc.
  • in awk, gsub is replacing original text with colored text
  • \033 is the start of the ANSI code

Generating colors with bash

There are other ways to create colors, for example using bash. The last script on this page would generate color table like this below:

colortablewithbash

 

Viewing session log afterwards in color

It is possible to visualize these colors afterwards, if the session is saved into a log file. This is done by converting those ansi escape codes into html colors. Please see another blog for details.

This entry was posted in Android, Anvanced Tools, Raspberry Pi, Tips, Tools, Troubleshooting. Bookmark the permalink.

Comments are closed.