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:
|
|
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:
|
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:
NOTE: The -e option of the echo command enables parsing of the escape sequences.echo -e "\e[1;31m\H \e[31\u\e[00m"
echo -e "\[\033[1;31m\]\H \[\033[0;31m\]\u\[\033[00m\]"
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:
|
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 echo4. 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.