Introduction
While working, maintaining, developing or playing on Linux servers, it can be very useful to use a dynamic MotD generator script. A MotD generator can show you some basic information about the system you are logging into over SSH or via the console. Over the past years I’ve been working on the ultimate MotD generator which can be tuned with themes and easily extended. Not having to manually check for available yum updates, check the server load or how many users are loged on can really save yourself some valuable time.
In order to know for sure you are working on a Bash shell, you can check /etc/passwd which contains one line for each user. The last column lists the default shell for each user. If this last field for your user is /bin/bash you know for sure you are working in a Bash shell.
I finally managed to get Cmder working with 256 colors. Just download and install Babun and make a task in Cmder where you link to mintty.exe.
Bash Colors
Most terminals are not just able to display black and white, they can also display colors thanks to escape sequences which are composed of an escape character (in Bash backslash \ ) followed by some other characters. For using one of the 256 colors on the foreground or text color, the control sequence is:
\e[0;<colornumber>m

If you want to edit one of the 256 colors on the background, the control sequence is:
\e]48;5<colornumber>m

You can find more information about terminals and colors here.
I’ve only had the chance to test the MotD on CentOS and Red Hat 6 and 7 servers, but it should also work on different distributions. If you would discover an issue on a different distribution, please create an issue on GitHub and I’ll try to make a fix for it.
I started with a fancy 256 color version, but discovered some terminal emulators such as Conemu in Cmder are not able to display 256 colors over SSH. With the help of some Cmder power users in this GitHub issue, I was pointed out that the development version of Cmder as an integrated mintty thanks to Babun. So I’ve been working to re-integrate the original 256 color script into the main script. So at this moment there are three possible themes.
The ‘Original’ theme (and the only theme for now with 256 colors) of the MotD generator script looks like this:

The 16 color Blue theme of the MotD generator look like this:

The 16 color Red theme of the MotD generator look like this:

How to use MotD generator?
If you want the scrip to show you the number of available yum updates, you will need to make a cron job, that runs once a day, counts each update and outputs to a textfile /tmp/yum_updates.txt
Put generate_motd.sh in /usr/local/nagios/libexec and make it executable. Configure your cronjob like this:
|
|
00 0 * * * /usr/local/nagios/libexec/generate_motd.sh yum > /tmp/yum_updates.txt |
You could of course place generate_motd.sh anywhere you see fit. Make generate_motd.sh script executable and do a couple of runs to make sure it works fine.
Please note the script has some dependencies. The following yum commands should fix that:
|
|
yum install openssh-clients yum install bc yum install sysstat |
Create the initialization script init.sh which will call the generate_motd.sh script in /etc/profile.d when logging in.
You can put whatever you like in this init.sh script. everything in it will be executed at the moment someone logs in your system. Mine looks like this on production servers:
|
|
#!/bin/bash /usr/local/bin/generate_motd.sh Red |
And like this on QA and DEV servers:
|
|
#!/bin/bash /usr/local/bin/generate_motd.sh Blue |
Make a new ssh connection, login and you should see your dynamic motd.
MotD Generator Components
Script Version
To make it easy to see what version of generate_motd.sh you are using, I integrated the version in the frame of the MotD.
|
|
ScriptName="`readlink -e $0`" ScriptVersion=" `cat $ScriptName | grep "# Version:" | awk {'print $3'} | tr -cd '[[:digit:].-]' | sed 's/.\{2\}$//'` " |
Ip
I changed the method of retrieving the ip address. As CentOS 7 has different naming conventions, it seem a more solid method to use ip route.
|
|
ip route get 8.8.8.8 | head -1 | cut -d' ' -f8 |
Release
It’s always useful to know what Linux release your are working on.
|
|
cat /etc/*release | head -n 1 |
Kernel
Knowing the exact kernel version can also contribute to learning to know your servers
Uptime
I found this command to be working best on most systems. It is also capable of showing the number of days, hours, minutes and seconds. I also found it more useful then just using ‘uptime‘.
|
|
awk '{print int($1/86400)" day(s) "int($1%86400/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime |
Average CPU Utilization
CPU utilization is a little harder as you prefer to get the average CPU utilization. To achieve this I’m using mpstat.
|
|
CpuUtil=`LANG=en_GB.UTF-8 mpstat 1 1 | awk '$2 ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } $2 ~ /all/ { print 100 - $field}' | tail -1` |
I also prefer to see the number of cores in the output:
|
|
CpuProc="`cat /proc/cpuinfo | grep processor | wc -l` core(s)." |
Detect virtualization technique
This new feature is still a bit experimental, but I found an article which showed me how to retrieve this information with dmesg. As I don’t have any physical machines available, I’m not 100 % sure what te result would be on bare metal.
-
Qemu with KVM
|
|
Platform=`dmesg | grep "DMI:" | sed 's/^.*QEMU/QEMU/' | sed 's/, B.*//'` |
- VMware
|
|
Platform=`dmesg | grep "DMI:" | sed 's/^.*VMware/VMware/' | sed 's/, B.*//'` |
- Fujitsu Primergy
|
|
Platform=`dmesg | grep "DMI:" | sed 's/^.*FUJITSU PRIMERGY/Fujitsu Primergy/' | sed 's/, B.*//'` |
So as I’m still finding out what this does on physical systems other then Fujitsu, you could get an ‘Unknown’ result. Please let me know on GitHub by making an issue. You should be able to find the correct platform with dmesg, cut and sed.
Playing with colors
In order to make it easier for you to play around with colors, just in case you would like to make your own theme, I added a little bash script colortest.sh, which will output all available colors on your terminal. The output should look similar to this:

Cowsay and other ASCII generators
Something old school that exists for a really long time is Cowsay, which is a configurable speaking ASCII cow. I’ve seen several people integrate it in their MotD, but personally I think it does not fit into an enterprise. Something to play with at home though. You can even make it say a random expression every time you log in. Here you can find an implementation of Cowsay where you can output to html, text or even Json. And here you can find a Python implmentation of Cowsay.
I also stumbled on Christopher Johnson’s ASCII Art Collection , an ascii archive containing a massive amount of ASCII art examples. The website is kind of outdated, but check it out if you like ascii art or if you are looking for something to integrate into your MotD.
If you want convert some text to ASCII, check out this website or this one where you can enter some text, choose a font, character width and height and see the result immediately. If you want to convert an image to ASCII, check out this website.
(Almost) Final words
It might seem like a small tool, but my MotD generator can really help you get a quick overview of your Linux server state. I’ll try to extend the tool when I find the time, as I still have some ideas left which can improve it.
Let me know on the Nagios Exchange what you think of my plugin by rating it or submitting a review. Please also consider starring the project on GitHub.