{"id":1883,"date":"2016-04-07T12:09:00","date_gmt":"2016-04-07T09:09:00","guid":{"rendered":"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1883"},"modified":"2016-04-07T23:39:52","modified_gmt":"2016-04-07T20:39:52","slug":"utilizing-color-in-our-ssh-session-example-with-android","status":"publish","type":"post","link":"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1883","title":{"rendered":"Utilizing color in ssh session (example with Android)"},"content":{"rendered":"<p>When using ssh to connect to &#8220;small&#8221; 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.<\/p>\n<p>This tip is showing how to use awk to use <span style=\"COLOR: red\">color<\/span> 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 <a href=\"https:\/\/en.wikipedia.org\/wiki\/ANSI_escape_code\">ANSI escape codes<\/a>. The terminal software then interprets these codes and shows the colors.<\/p>\n<p>In case of android, tool called awk is included in <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=stericson.busybox&amp;hl=en\">busybox<\/a> or <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=berserker.android.apps.sshdroid&amp;hl=en\">sshdroid<\/a> installation.<\/p>\n<p>What is needed:<\/p>\n<ul>\n<li>awk in android (included in <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=berserker.android.apps.sshdroid&amp;hl=en\">sshdroid<\/a>)<\/li>\n<li>ssh connection (using <a href=\"http:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/download.html\">putty<\/a>)<\/li>\n<\/ul>\n<p><strong>Coloring <span style=\"TEXT-DECORATION: underline\">words<\/span>, example with command: mount<\/strong> <\/p>\n<p><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-mount.png\"><img loading=\"lazy\" alt=\"android-awk-mount\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-mount-small.png\" width=\"450\" height=\"308\" \/><\/a><\/p>\n<p>The command, including the awk script, is shown below. Awk is used to highlight words with colors.<\/p>\n<div style=\"OVERFLOW: auto; WIDTH: 550px\">mount | awk &#8216;BEGIN {IGNORECASE=1;<br \/>\nblack =sprintf(&#8220;\\033[1;30;40m&amp;\\033[0m&#8221;);<br \/>\nred =sprintf(&#8220;\\033[1;31;40m&amp;\\033[0m&#8221;);<br \/>\ngreen =sprintf(&#8220;\\033[1;32;40m&amp;\\033[0m&#8221;);<br \/>\nyellow =sprintf(&#8220;\\033[1;33;40m&amp;\\033[0m&#8221;);<br \/>\nblue =sprintf(&#8220;\\033[1;34;40m&amp;\\033[0m&#8221;);<br \/>\npurple =sprintf(&#8220;\\033[1;35;40m&amp;\\033[0m&#8221;);<br \/>\ncyan =sprintf(&#8220;\\033[1;36;40m&amp;\\033[0m&#8221;);<br \/>\nwhite =sprintf(&#8220;\\033[1;37;40m&amp;\\033[0m&#8221;);};<br \/>\n{gsub( $1,blue ,$1);<br \/>\ngsub( $2,yellow ,$2);<br \/>\ngsub( $3,cyan ,$3);<br \/>\ngsub(&#8220;rw,&#8221;,red ,$0);<br \/>\ngsub(&#8220;ro,&#8221;,green ,$0);<br \/>\nprint}&#8217;<\/div>\n<p><strong>Coloring <span style=\"TEXT-DECORATION: underline\">lines<\/span>, example with command: logcat<\/strong><\/p>\n<p><strong><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-logcat.png\"><img loading=\"lazy\" alt=\"android-awk-logcat\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-logcat-small.png\" width=\"450\" height=\"307\" \/><\/a><\/strong><\/p>\n<p>The command, including awk script is shown below. Awk is used to highlight full lines.<\/p>\n<div style=\"OVERFLOW: auto; WIDTH: 550px\">logcat | awk &#8216;BEGIN {IGNORECASE=1};<br \/>\nfunction black(string) { print &#8220;\\033[1;30m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction red(string) { print &#8220;\\033[1;31m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction green(string) { print &#8220;\\033[1;32m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction yellow(string) { print &#8220;\\033[1;33m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction blue(string) { print &#8220;\\033[1;34m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction purple(string) { print &#8220;\\033[1;35m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction cyan(string) { print &#8220;\\033[1;36m&#8221; string &#8220;\\033[0m &#8220;; }<br \/>\nfunction white(string) { print &#8220;\\033[1;37m&#8221; string &#8220;\\033[0m &#8220;;<br \/>\n{if ($0 ~ \/location\/) red($0);<br \/>\nelse if ($0 ~ \/LocSvc\/) red($0);<br \/>\nelse if ($0 ~ \/wifi\/) yellow($0);<br \/>\nelse if ($0 ~ \/WLAN\/) yellow($0);<br \/>\nelse if ($0 ~ \/connectivity\/) yellow($0);<br \/>\nelse if ($0 ~ \/google\/) blue($0);<br \/>\nelse if ($0 ~ \/browser\/) blue($0);<br \/>\nelse if ($0 ~ \/root\/) cyan($0);<br \/>\nelse if ($0 ~ \/supersu\/) cyan($0);<br \/>\nelse if ($0 ~ \/busybox\/) cyan($0);<br \/>\nelse print $0}&#8217;<\/div>\n<p>\n<strong>Coloring <span style=\"TEXT-DECORATION: underline\">words<\/span>, example with command: logcat<\/strong><\/p>\n<p><strong><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-logcat-words.png\"><img loading=\"lazy\" alt=\"android-awk-logcat-words\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-logcat-words-small.png\" width=\"450\" height=\"308\" \/><\/a><\/strong><\/p>\n<p>And the code:<\/p>\n<div style=\"OVERFLOW: auto; WIDTH: 450px\">logcat | awk &#8216;BEGIN {<br \/>\nIGNORECASE=1;<br \/>\nred =sprintf(&#8220;\\033[1;31;40m&amp;%c\\033[0m&#8221;, 0x1B);<br \/>\nyellow =sprintf(&#8220;\\033[1;33;40m&amp;%c\\033[0m&#8221;, 0x1B);<br \/>\nblue =sprintf(&#8220;\\033[1;34;40m&amp;%c\\033[0m&#8221;, 0x1B);};<br \/>\n{gsub(&#8220;location&#8221;,red);<br \/>\ngsub(&#8220;wifi&#8221; ,yellow);<br \/>\ngsub(&#8220;google&#8221; ,blue);<br \/>\nprint}&#8217;<\/div>\n<p>\n<strong>Coloring <span style=\"TEXT-DECORATION: underline\">words<\/span>\/columns, example with command: top<\/strong><\/p>\n<p><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-top.png\"><img loading=\"lazy\" alt=\"android-awk-top\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-top-small.png\" width=\"450\" height=\"307\" \/><\/a><\/p>\n<p>And the code:<\/p>\n<div style=\"OVERFLOW: auto; WIDTH: 450px\">top -b | head -30 | awk &#8216;BEGIN<br \/>\n{IGNORECASE=1;<br \/>\nblack =sprintf(&#8220;\\033[1;30;40m&amp;\\033[0m&#8221;);<br \/>\nred =sprintf(&#8220;\\033[1;31;40m&amp;\\033[0m&#8221;);<br \/>\ngreen =sprintf(&#8220;\\033[1;32;40m&amp;\\033[0m&#8221;);<br \/>\nyellow =sprintf(&#8220;\\033[1;33;40m&amp;\\033[0m&#8221;);<br \/>\nblue =sprintf(&#8220;\\033[1;34;40m&amp;\\033[0m&#8221;);<br \/>\npurple =sprintf(&#8220;\\033[1;35;40m&amp;\\033[0m&#8221;);<br \/>\ncyan =sprintf(&#8220;\\033[1;36;40m&amp;\\033[0m&#8221;);<br \/>\nwhite =sprintf(&#8220;\\033[1;37;40m&amp;\\033[0m&#8221;);};<br \/>\n{gsub(&#8221; radio &#8220;,yellow,$0);<br \/>\ngsub(&#8221; system &#8220;,blue,$0);<br \/>\ngsub(&#8221; root &#8220;,red,$0);print}&#8217;<\/div>\n<p>\n<strong>Coloring <span style=\"TEXT-DECORATION: underline\">words<\/span>\/columns, example with command: ps<\/strong><\/p>\n<p><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-ps.png\"><img loading=\"lazy\" alt=\"android-awk-ps\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/android-awk-ps-small.png\" width=\"450\" height=\"308\" \/><\/a><\/p>\n<p>And the code:<\/p>\n<div style=\"OVERFLOW: auto; WIDTH: 450px\">ps -w | awk &#8216;BEGIN<br \/>\n{IGNORECASE=1;<br \/>\nblack =sprintf(&#8220;\\033[1;30;40m&amp;\\033[0m&#8221;);<br \/>\nred =sprintf(&#8220;\\033[1;31;40m&amp;\\033[0m&#8221;);<br \/>\ngreen =sprintf(&#8220;\\033[1;32;40m&amp;\\033[0m&#8221;);<br \/>\nyellow =sprintf(&#8220;\\033[1;33;40m&amp;\\033[0m&#8221;);<br \/>\nblue =sprintf(&#8220;\\033[1;34;40m&amp;\\033[0m&#8221;);<br \/>\npurple =sprintf(&#8220;\\033[1;35;40m&amp;\\033[0m&#8221;);<br \/>\ncyan =sprintf(&#8220;\\033[1;36;40m&amp;\\033[0m&#8221;);<br \/>\nwhite =sprintf(&#8220;\\033[1;37;40m&amp;\\033[0m&#8221;);};<br \/>\n{gsub(&#8221; radio &#8220;,yellow,$0);<br \/>\ngsub(&#8221; system &#8220;,blue,$0);<br \/>\ngsub(&#8221; root &#8220;,red,$0);print}&#8217;<\/div>\n<p>\n<strong>Some explanations<\/strong><\/p>\n<ul>\n<li>in awk, $0 means full line, $1 is the first field in the line, $2 is the second field etc.<\/li>\n<li>in awk, gsub is replacing original text with colored text<\/li>\n<li>\\033 is the start of the ANSI code<\/li>\n<\/ul>\n<p><strong>Generating colors with bash<\/strong><\/p>\n<p>There are other ways to create colors, for example using bash. The last script on this <a href=\"http:\/\/tldp.org\/HOWTO\/Bash-Prompt-HOWTO\/x329.html\">page<\/a> would generate color table like this below:<\/p>\n<p><a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/colortablewithbash.png\"><img loading=\"lazy\" alt=\"colortablewithbash\" src=\"http:\/\/saisa.eu\/blogs\/Guidance\/wp-content\/uploads\/2016\/04\/colortablewithbash-small.png\" width=\"450\" height=\"313\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Viewing session log afterwards in color<\/strong><\/p>\n<p>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 <a href=\"http:\/\/saisa.eu\/blogs\/Guidance\/?p=1902\">blog<\/a> for details.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using ssh to connect to &#8220;small&#8221; 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 &hellip; <a href=\"https:\/\/saisa.eu\/blogs\/Guidance\/?p=1883\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,17,82,10,7,20],"tags":[],"_links":{"self":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1883"}],"collection":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1883"}],"version-history":[{"count":12,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1883\/revisions"}],"predecessor-version":[{"id":1909,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=\/wp\/v2\/posts\/1883\/revisions\/1909"}],"wp:attachment":[{"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saisa.eu\/blogs\/Guidance\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}