LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-01-2012, 03:39 PM   #1
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Colorizing the pre-login banner for the console


I am trying to colorize the text that appears just before the login prompt when you drop to the console.

After reading through the manpages for getty and several other pages on the web, it seems that it may not be as simple as I thought.

The pre-login banner message which is in /etc/issue as read by getty must be a plain text file with a limited number of control codes as stated in the getty manpages. Unfortunately, these control codes does not include any codes for setting colors.

I am now thinking my last resort is to run a script which would display my banner in color just before getty runs. The problem is, after several hours of reading and searching, I cannot find exactly when getty is loaded after I invoke the console with <CTRL><ALT><F1>.

I tried doing it with inittab but it did not work. This is what I did. I created a simple script (/sbin/mygetty.sh) to display a colored message and call getty:
Code:
#! /bin/bash

echo "\033[1;34mHello"
/sbin/getty 38400 tty2
I then modified my inittab file for tty2 only to call my script instead of getty:
Code:
2:23:respawn:/sbin/mygetty.sh 38400 tty2
But after rebooting and switching to tty2, I only get a flashing underscore which tells me my script is not being run.

So after even further research, it appears that calling getty is much more complex than I thought.

My question is this: what calls getty when I switch to the console? I'm thinking I should run my script to display the colored banner just before getty is called.

Just another hurdle to become X-independent. Any guidance is greatly appreciated.
 
Old 06-01-2012, 10:18 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Original Poster
Rep: Reputation: 125Reputation: 125
Again, after looking at the initscripts and researching I've edited my script:
Code:
#!/bin/sh

echo -e "\033[1;34mHello" >&2
/sbin/getty -i 38400 tty2
exit 0
Now when I switch to TTY2, I just get the login prompt. However, if I login and logout, 'Hello' is now displayed in blue as expected.

So now, how do I get the banner to display when I first switch to TTY2?

Again, any help is greatly appreciated.
 
Old 06-10-2012, 02:07 PM   #3
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Original Poster
Rep: Reputation: 125Reputation: 125
Well, I finally got it working. I'll post the script here for those who may be interested and mark the thred as solved.
Here's the script I called mygetty.sh. In Debian, it's in /sbin but you can put it in the same directory as your getty program.
Code:
#!/bin/sh

# This script displays a custom pre-login banner message in a VT.
# Is uses the /etc/issue file to set the banner message. Each line
# can specify the attributes for the text on that line.
# The last line in the file can reset the attributes to their default,
# or set the attributes for the login prompt.

# $1 is the baud rate as set in /etc/inittab.
# $2 is the device designator for the VT as set in /etc/inittab.

# Version 1.0 
# Copyright (C) 2012 Towheed Mohammed

# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details at <http://www.gnu.org/licenses/>.

# The following are the command codes:
# [arch_name]       Insert the architecture of the machine.
# [baud_rate]       Insert the baud rate of the current line.
# [current_date]    Insert the current date.
# [current_time]    Insert the current time.
# [dns_name]        Insert the DNS domain name of the machine.
# [host_name]       Insert the nodename/hostname of the machine.
# [kernel_version]  Insert the version of the OS.
# [kernel_release]  Insert the release number of the OS.
# [nis_name]        Insert the NIS domain name of the machine.
# [system_name]     Insert the system name.
# [total_users]     Insert the number of current users logged in.
# [tty_name]        Insert the name of the current tty line.

# These are the codes for the text attributes.
# [color_normal]     Set the color attributes to normal.
# [bold_on]          Set the bold attribute.
# [bold_off]         Reset the bold attribute (normal intensity).
# [reverse_on]       Set reverse video.
# [reverse_off]      Reset reverse video.
# [black]            Set text color to black.
# [red]              Set text color to red.
# [green]            Set text color to green.
# [brown]            Set the text color to brown.
# [blue]             Set the text color to blue.
# [magenta]          Set the text color to magenta.
# [cyan]             Set the text color to cyan.
# [white]            Set the text color to white.
# [black_bkg]        Set the background color to black.
# [red_bkg]          Set the background color to red.
# [green_bkg]        Set the background color to green.
# [brown_bkg]        Set the background color to brown.
# [blue_bkg]         Set the background color to blue.
# [magenta_bkg]      Set the background color to magenta.
# [cyan_bkg]         Set the background color to cyan.
# [white_bkg]        Set the background color to white.

# Color attributes variable assignment.
normal='\\033[0m'
bold_on='\\033[1m'
bold_off='\\033[22m'
reverse_on='\\033[7m'
reverse_off='\\033[27m'

# Foreground colors.
black='\\033[30m'
red='\\033[31m'
green='\\033[32m'
brown='\\033[33m'
blue='\\033[34m'
magenta='\\033[35m'
cyan='\\033[36m'
white='\\033[37m'

# Background colors
black_bkg='\\033[40m'
red_bkg='\\033[41m'
green_bkg='\\033[42m'
brown_bkg='\\033[43m'
blue_bkg='\\033[44m'
magenta_bkg='\\033[45m'
cyan_bkg='\\033[46m'
white_bkg='\\033[47m'

# Assign variables
tdate="`date -d now +\"%a %b %d %Y\"`"
ltime="`date -d now +\"%I:%M:%S %p\"`"
ttyname="$2"
osname="`uname -s`"
arch="`arch`"
host_name="`hostname`"
kernel_version="`uname -v`"
kernel_release="`uname -r`"
nusers="`who -u | wc -l`"
nisname="`hostname -y`"
dnsname="`hostname -d`"
baudrate="`stty -F /dev/$2 speed`"

#cclist="arch_name \
#        baud_rate \
#        current_date \
#        current_time \
#        dns_name \
#        host_name \
#        kernel_version \
#        kernel_release \
#        nis_name \
#        system_name \
#        total_users \
#        tty_name \
#        color_normal \
#        bold_on \
#        bold_off \
#        reverse_on \
#        reverse_off \
#        black \
#        red \
#        green \
#        brown \
#        blue \
#        magenta \
#        cyan \
#        white \
#        black_bkg \
#        red_bkg \
#        green_bkg \
#        brown_bkg \
#        blue_bkg \
#        magenta_bkg \
#        cyan_bkg \
#        white_bkg"

#declare -A cmdlist
#cmdlist=( ["arch_name"]="`arch`" \
#          ["baud_rate"]="`stty -F /dev/$2 speed`" \
#          ["current_date"]="`date -d now +\"%a %b %d %Y\"`" \
#          ["current_time"]="`date -d now +\"%I:%M:%S %p\"`" \
#          ["dns_name"]="`hostname -d`" \
#          ["host_name"]="`hostname`" \
#          ["kernel_version"]="`uname -v`" \
#          ["kernel_release"]="`uname -r`" \
#          ["nis_name"]="`hostname -y`" \
#          ["system_name"]="`uname -s`" \
#          ["total_users"]="`who -u | wc -l`" \
#          ["tty_name"]="`tty | sed 's,.*/,,'`" \
#          ["color_normal"]='\\033[0m' \
#          ["bold_on"]='\\033[1m' \
#          ["bold_off"]='\\033[22m' \
#          ["reverse_on"]='\\033[7m' \
#          ["reverse_off"]='\\033[27m' \
#          ["black"]='\\033[30m' \
#          ["red"]='\\033[31m' \
#          ["green"]='\\033[32m' \
#          ["brown"]='\\033[33m' \
#          ["blue"]='\\033[34m' \
#          ["magenta"]='\\033[35m' \
#          ["cyan"]='\\033[36m' \
#          ["white"]='\\033[37m' \
#          ["black_bkg"]='\\033[40m' \
#          ["red_bkg"]='\\033[41m' \
#          ["green_bkg"]='\\033[42m' \
#          ["brown_bkg"]='\\033[43m' \
#          ["blue_bkg"]='\\033[44m' \
#          ["magenta_bkg"]='\\033[45m' \
#          ["cyan_bkg"]='\\033[46m' \
#          ["white_bkg"]='\\033[47m' )

# Reset the VT.
echo -e "\033c" >/dev/$2

# Read /etc/issue and echo each line to the specified VT.

# The while loop is much slower at processing /etc/issue
# and printing the text on the VT.  On my machine it took
# approx 880mS to display the sample file. What appears to
# be the convulated sed statement took just 94mS.

# I've left the while loop for those who would prefer it.
#while read -r line; do
#  for code in $cclist; do
    #line=`echo $line | sed 's,\['"$code"'],'"${cmdlist[$code]}"',g'`
#  done
#  echo -e "$line" >/dev/$2
#done < /etc/issue
#unset $cmdlist

# This is much faster than the while loop. If you use the
# while loop, delete this.
echo -e "`sed -e 's,\[arch_name],'"$arch"',g' \
-e 's,\[baud_rate],'"$baudrate"',g' \
-e 's,\[current_date],'"$tdate"',g' \
-e 's,\[current_time],'"$ltime"',g' \
-e 's,\[dns_name],'"$dnsname"',g' \
-e 's,\[host_name],'"$host_name"',g' \
-e 's,\[kernel_version],'"$kernel_version"',g' \
-e 's,\[kernel_release],'"$kernel_release"',g' \
-e 's,\[nis_name],'"$nisname"',g' \
-e 's,\[system_name],'"$osname"',g' \
-e 's,\[total_users],'"$nusers"',g' \
-e 's,\[tty_name],'"$ttyname"',g' \
-e 's,\[color_normal],'"$normal"',g' \
-e 's,\[bold_on],'"$bold_on"',g' \
-e 's,\[bold_off],'"$bold_off"',g' \
-e 's,\[reverse_on],'"$reverse_on"',g' \
-e 's,\[reverse_off],'"$reverse_off"',g' \
-e 's,\[black],'"$black"',g' \
-e 's,\[red],'"$red"',g' \
-e 's,\[green],'"$green"',g' \
-e 's,\[brown],'"$brown"',g' \
-e 's,\[blue],'"$blue"',g' \
-e 's,\[magenta],'"$magenta"',g' \
-e 's,\[cyan],'"$cyan"',g' \
-e 's,\[white],'"$white"',g' \
-e 's,\[black_bkg],'"$black_bkg"',g' \
-e 's,\[red_bkg],'"$red_bkg"',g' \
-e 's,\[green_bkg],'"$green_bkg"',g' \
-e 's,\[brown_bkg],'"$brown_bkg"',g' \
-e 's,\[blue_bkg],'"$blue_bkg"',g' \
-e 's,\[magenta_bkg],'"$magenta_bkg"',g' \
-e 's,\[cyan_bkg],'"$cyan_bkg"',g' \
-e 's,\[white_bkg],'"$white_bkg"',g' </etc/issue`" >/dev/$2

# Change this line to the correct path and name of your getty.
exec /sbin/getty -i $1 $2
exit 0
This is a test /etc/issue file I used for testing the control code:
Code:
You are connected on [bold_on][red][tty_name] [color_normal]at [bold_on][magenta][baud_rate][white] bauds.
[bold_on][blue]Today is [red][current_date] [blue]and the local time is [red][current_time].
[green]My DNS name is [red][dns_name].
[cyan]This machine is an [arch_name] running [brown][system_name] [kernel_release] [kernel_version].
[white]There are [blue][total_users] [white]users logged on.

Normal text:
[bold_off][black]This text is black  [red]This text is red  [green]This text is green  [brown]This text is brown
[blue]This text is blue  [magenta]This text is magenta  [cyan]This text is cyan  [white]This text is white

Bold text:
[bold_on][black]This text is black  [red]This text is red  [green]This text is green  [brown]This text is brown
[blue]This text is blue  [magenta]This text is magenta  [cyan]This text is cyan  [white]This text is white[bold_off]

Reverse text:
[red_bkg][black]Black on red[black_bkg] [red_bkg][green]Green on red[black_bkg]  [red_bkg][brown]Brown on red[black_bkg]  [red_bkg][blue]Blue on red[black_bkg]  [red_bkg][magenta]Magenta on red[black_bkg]  [red_bkg][cyan]Cyan on red[black_bkg]  [red_bkg][white]White on red
[green_bkg][black]Black on green[black_bkg] [green_bkg][red]Red on green[black_bkg]  [green_bkg][brown]Brown on green[black_bkg]  [green_bkg][blue]Blue on green[black_bkg]  [green_bkg][magenta]Magenta on green[black_bkg]  [green_bkg][cyan]Cyan on green[black_bkg]  [green_bkg][white]White on green
[brown_bkg][black]Black on brown[black_bkg] [brown_bkg][red]Red on brown[black_bkg]  [brown_bkg][green]Green on brown[black_bkg]  [brown_bkg][blue]Blue on brown[black_bkg]  [brown_bkg][magenta]Magenta on brown[black_bkg]  [brown_bkg][cyan]Cyan on brown[black_bkg]  [brown_bkg][white]White on brown
[blue_bkg][black]Black on blue[black_bkg] [blue_bkg][red]Red on blue[black_bkg]  [blue_bkg][brown]Brown on blue[black_bkg]  [blue_bkg][magenta]Magenta on blue[black_bkg]  [blue_bkg][cyan]Cyan on blue[black_bkg]  [blue_bkg][white]White on blue
[magenta_bkg][black]Black on magenta[black_bkg]  [magenta_bkg][red]Red on magenta[black_bkg]  [magenta_bkg][green]Green on magenta[black_bkg]  [magenta_bkg][brown]Brown on magenta[black_bkg]  [magenta_bkg][blue]Blue on magenta[black_bkg]  [magenta_bkg][cyan]Cyan on magenta[black_bkg]  [magenta_bkg][white]White on magenta
[cyan_bkg][black]Black on cyan[black_bkg]  [cyan_bkg][red]Red on cyan[black_bkg]  [cyan_bkg][green]Green on cyan[black_bkg]  [cyan_bkg][brown]Brown on cyan[black_bkg]  [cyan_bkg][blue]Blue on cyan[black_bkg]  [cyan_bkg][magenta]Magenta on cyan[black_bkg]  [cyan_bkg][white]White on cyan
[white_bkg][black]Black on white[black_bkg]  [white_bkg][red]Red on white[black_bkg]  [white_bkg][green]Green on white[black_bkg]  [white_bkg][brown]Brown on white[black_bkg]  [white_bkg][blue]Blue on white[black_bkg]  [white_bkg][magenta]Magenta on white[black_bkg]  [white_bkg][cyan]Cyan on white

[black]This line shows that whitespace are drawn in the last background color.

[bold_on][green]If attributes are not reset, the last attributes will be applied to the login prompt
You must now modify you /etc/inittab. I would suggest you test it on one VT first. to test on TTY2, change this line of your /etc/inittab:
Code:
2:23:respawn:/sbin/getty.sh 38400 tty2
to:
Code:
2:23:respawn:/sbin/mygetty.sh 38400 tty2
Now have init re-examine the /etc/inittab file:
Code:
sudo init -Q
Now kill all getty processes. This will respawn all getty's except for the one that was used for TTY2:
Code:
sudo killall getty
Switching to TTY2 will now show your colorized pre-login banner.

Last edited by towheedm; 06-11-2012 at 11:45 AM. Reason: Added exec call to getty.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to setup system Login banner and Login message 06-03-05 Linux - Newbie 13 07-24-2020 02:05 PM
SSH login banner/No root login jmoschetti45 Linux - Security 3 01-17-2010 04:51 PM
Pre-login GUI banner kpraveen455 Linux - Desktop 2 05-10-2009 03:53 AM
Need pre-login banner/text, how to setup? Compuwiz Linux - Security 6 08-30-2006 01:37 AM
Re posted: Colorizing OpenBSD Console gani *BSD 2 09-14-2005 04:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration