My-Tiny.Net :: Networking with Virtual Machines



ANSI Escape Sequences



/etc/issue comes up every time we log in. It is plain text, not a script but it shows us how to use "ANSI/VT100 escape sequences" for text effects.

ANSI escape sequences are non-printed text that is interpreted to change the format of some text. Their support depends on the terminal, but the color control sequences are commonly supported.

There are eight colors numbered zero to seven. The color number must be followed by m
The number preceding the actual color code indicates 0 for normal, 1 for bold.

Foreground color escape sequences:
Black0;30m
Red0;31m
Green0;32m
Yellow0;33m
Blue0;34m
Purple0;35m
Cyan0;36m
Light Gray0;37m
Dark Gray1;30m
Bold Red1;31m
Bold Green1;32m
Bold Yellow1;33m
Bold Blue1;34m
Bold Purple1;35m
Bold Cyan1;36m
White1;37m
For background colors use
4_m instead of 3_m.
So to have the background
blue instead of the text, use
0;44m instead of 0;34m.

also:
00m resets all
39m resets foreground color
49m resets background color


Bash shell escape sequences consist of a backslash (the standard escape character) followed by a regular character.
While there are many available, some common ones are:

\ean ASCII escape character (033)
\nnnthe character with ASCII code octal value nnn
\[begin a sequence of non-printing characters (like color escape sequences)
\]end a sequence of non-printing characters (allows bash to calculate word wrapping correctly)
\\A backslash
\dThe date, in "WMD" format (e.g., Tue May 26)
\hThe hostname, up to the first dot (e.g. gateway)
\HThe hostname. (e.g. gateway.my-tiny.net)
\tThe time, in 24-hour HH:MM:SS format
\TThe time, in 12-hour HH:MM:SS format
\@The time, in 12-hour am/pm format
\uThe username of the current user
\$$ if you are not root, or # if you are (root uid = 0)
\nA newline
\wThe current working directory ($PWD)
\WThe basename of $PWD


Note that \w or \W will always display ~ for the home directory instead of expanding it, and there really is no way to change that.

For color codes, normal (non-bold) foreground is the default in Bash, and the 0; prefix is optional. So, the following modifications are equivalent, but without \] after the change you may see strange behaviors with line wrapping:

echo -e "\e[1;31m\H \e[31\u\e[00m"

echo -e "\[\033[1;31m\]\H \[\033[0;31m\]\u\[\033[00m\]"
NOTE: The -e option of the echo command enables parsing of the escape sequences.

For a complete list of codes to change display graphics, control cursor movement, and reassign keys, see
http://ascii-table.com/ansi-escape-sequences.php

getty and /etc/issue

/etc/issue comes up every time we log in. It is plain text, not a script, and is displayed by getty (short for "get tty"), a program that manages physical and virtual terminals (TTYs). When it detects a connection, it prompts for a username, runs the 'login' program to authenticate the user, and displays /etc/issue after a successful login.

Since /etc/issue is displayed by getty rather than the shell, some of the escape codes are slightly different. The [ESC] in the MC editor will appear as ^[ in other editors. Since /etc/issue is plain text, not a script, we need to create this character with echo -e "\x1b" in a script. This way, any ANSI escape sequence will work as expected.

Also, getty pays special attention to backslashes (\), so they have to be doubled (\\) to show one. Backslashes often are used in ASCII art images, so doubling them up breaks the layout when anything except getty displays it.

Finally, getty only has a few predefined codes for information:

\dcurrent date
\tcurrent time
\odomainname of the machine
\nnodename (hostname) of the machine
\ssystem name, e.g., the name of the operating system
\marchitecture identifier of the machine, e.g., i686
\rrelease number of the kernel, e.g., 2.6.11.12
\vversion of the OS, e.g., the build-date etc.
\bbaudrate of the current tty line
\lname of the current tty line
\unumber of current users logged in
\U"1 user" or \u" users"


So, to get ascii art into /etc/issue

1. use figlet or something to create your art

     __     _            _____ _                    __     _   
  /\ \ \___| |_    _ __ /__   (_)_ __  _   _     /\ \ \___| |_ 
 /  \/ / _ \ __|  | '__|  / /\/ | '_ \| | | |   /  \/ / _ \ __|
/ /\  /  __/ |_ []| |    / /  | | | | | |_| |  / /\  /  __/ |_ 
\_\ \/ \___|\__|  |_|  ()_/   |_|_| |_|\__, |()__\ \/ \___|\__|
                                       |___/                    
2. use the global replace function in your editor to double up the backslashes -- or for cool ways to do this with regular expressions, check out http://unix.stackexchange.com/questions/229585/sed-convert-single-backslash-to-double-backslash

     __     _            _____ _                    __     _   
  /\\ \\ \\___| |_    _ __ /__   (_)_ __  _   _     /\\ \\ \\___| |_ 
 /  \\/ / _ \\ __|  | '__|  / /\\/ | '_ \\| | | |   /  \\/ / _ \\ __|
/ /\\  /  __/ |_ []| |    / /  | | | | | |_| |  / /\\  /  __/ |_ 
\\_\\ \\/ \\___|\\__|  |_|  ()_/   |_|_| |_|\\__, |()__\\ \\/ \\___|\\__|
                                       |___/                    
3. make a shell script to create /etc/issue

#!/bin/bash

# home the cursor and clear the the screen
  echo -e "\x1b[H\x1b[2J" >/etc/issue

# display in bold blue
  echo -e "\x1b[1;34m" >>/etc/issue

# my double-backslashed ascii art
  cat /etc/gettynetr-logo >>/etc/issue

# we could use getty escapes, but this is too easy
  echo -e "\x1b[1;37m" >>/etc/issue
  cat /etc/HOSTNAME >>/etc/issue

# we have to do the IP ourselves
  LN=$(ifconfig |grep -v 127.0.0.1 |grep inet |grep -v inet6)
  IA=$(echo $LN |cut -d " " -f 2 |cut -d ":" -f 2)
  NM=$(echo $LN |cut -d " " -f 4 |cut -d ":" -f 2)

  echo -e "\x1b[1;32m     " $IA "\x1b[0;32m    NetMask " $NM  >>/etc/issue

# back to normal color
echo -e "\x1b[0m" >>/etc/issue
echo 

4. call the script in /etc/rc.d/rc.local if you are using DHCP, or just run it once if you use static IP addresses.