Show System Stats after you Login to the Shell

How would you like to display tons of useful data when you login to your machine? To show you how the system is doing, along with even the weather? And to show the texts in red if there’s an issue? Something like:

image-center

Well, you can, and it’s relatively easy.

The scripts

We are going to make this happen by adding in some files and scripts.

You probably notice the awesome SuperTechCrew logo. You can make yours too. It’s simply generated using the program figlet (which you can install using most distro package managers).

It has TONS of ways it can manipulate the text, using the -f option. To see all the ones you can use, run:

figlet -I2 | xargs -l

Just remove the .flc from the name. For mine, I used slant:

figlet -f slant supertechcrew

On the other hand, if you want to see ALL the different fonts, and how your logo/name looks, just run this, substituting ‘NAME’ with your own):

figlist | while read type ; do echo $type ; figlet -f $type NAME ; done

(If you get some lines saying figlet: Figlet: Unable to open font file, just ignore those fonts).

There’s lots of other things figlet can do. See man figlet.

The Real Deal

Here is the main script that displays the system stats. (It will run with the permissions of the user that is logging in, but more on that below.) You’ll want to replace [ Put your logo here ] with the logo you generated above, or you can remove that line and just and no logo. Boring, but it’ll work too.

Save it to /etc/motd.tcl

#!/usr/bin/env tclsh
# Modified by Matt Bagley. Original from from mewbies.com (v.03 2013 Sep 01)
# Last updated Oct 18, 2016

# * Variables
set var(user) $env(USER)
set var(path) $env(PWD)
set var(home) $env(HOME)

# * Calculate last login
set lastlog [exec -- lastlog -u $var(user)]
set ll(1)  [lindex $lastlog 7]
set ll(2)  [lindex $lastlog 8]
set ll(3)  [lindex $lastlog 9]
set ll(4)  [lindex $lastlog 10]
set ll(5)  [lindex $lastlog 6]

# * Calculate current system uptime
set uptime    [exec -- /usr/bin/cut -d. -f1 /proc/uptime]
set up(days)  [expr {$uptime/60/60/24}]
set up(hours) [expr {$uptime/60/60%24}]
set up(mins)  [expr {$uptime/60%60}]
set up(secs)  [expr {$uptime%60}]

# * Calculate current disk usage
set usage [lindex [exec -- /usr/bin/df -h / | tail -n +2] 4]

# * Calculate usage of home directory
#set homeusage [lindex [exec -- /usr/bin/du -ms $var(home)] 0]

# * Calculate SSH logins:
set logins     [exec -- w -s]
set log(c)  [lindex $logins 5]

# * Calculate processes
set psa [expr {[lindex [exec -- ps -A h | wc -l] 0]-000}]
set psu [expr {[lindex [exec -- ps U $var(user) h | wc -l] 0]-002}]
set verb are
#if [expr $psu < 2] {
#	if [expr $psu = 0] {
#		set psu none
#	} else {
#		set verb is
#		}
#}

# * Calculate current system load
set loadavg     [exec -- /bin/cat /proc/loadavg]
set sysload(1)  [lindex $loadavg 0]
set sysload(5)  [lindex $loadavg 1]
set sysload(15) [lindex $loadavg 2]

# * Calculate Memory
set memory  [exec -- free -m]
set mem(t)  [lindex $memory 7]
set mem(u)  [lindex $memory 8]
set mem(f)  [lindex $memory 9]
set mem(c)  [lindex $memory 11]
set mem(s)  [lindex $memory 15]

# * Calculate disk temperature from hddtemp
#set hddtemp [lindex [exec -- /usr/bin/hddtemp /dev/sda -uf | cut -c "31-35"] 0]

# * Calculate temperature from lm-sensors
#set temperature    [exec -- sensors -f | grep °F | tr -d '+']
#set tem(0)  [lindex $temperature 2]
#set tem(m)  [lindex $temperature 4]
#set tem(c)  [lindex $temperature 15]

# * Display weather
set weather     [exec -- /usr/local/bin/weather-display]
#set wthr(t)  [lindex $weather 0]
#set wthr(d)  [lindex $weather 1]
#set wthr(e)  [lindex $weather 2]
set now       [exec -- echo $weather | head -n 1 ]
set today     [exec -- echo $weather | head -n 2 | tail -n 1 ]
set tomorrow  [exec -- echo $weather | tail -n 1 ]

# * ASCII head
set head {
        
        
        [ Put your logo here ]
        
        
}

# * Print Output
puts "\033\[01;32m$head\033\[0m"
puts "  \033\[35mLast Login....:\033\[0m \033\[36m$ll(1) $ll(2) $ll(3) $ll(4) from\033\[0m \033\[33m$ll(5)\033\[0m"
puts "  \033\[35mUptime........:\033\[0m \033\[36m$up(days) days $up(hours) hours $up(mins) minutes $up(secs) seconds\033\[0m"
#puts "  \033\[35mLoad..........:\033\[0m \033\[36m$sysload(1) (1minute) $sysload(5) (5minutes) $sysload(15) (15minutes)\033\[0m"
puts "  \033\[35mLoad..........:\033\[0m \033\[36m$sysload(1) / $sysload(5) / $sysload(15) (1min/5min/15min)\033\[0m"
puts "  \033\[35mMemory........:\033\[0m \033\[36mUsed: $mem(u)MB/$mem(t)MB  Free: $mem(f)MB  Cached: $mem(c)MB  "
puts "                  Swap In Use: $mem(s)MB\033\[0m"
#puts "  \033\[35mTemperature...:\033\[0m \033\[36mCore0: $tem(0)  M/B: $tem(m)  CPU: $tem(c)  Disk: ${hddtemp}\033\[0m"
#puts "  \033\[35mDisk Usage....:\033\[0m \033\[36mYou're using ${homeusage}MB in $var(home)\033\[0m"
puts "  \033\[35mDisk Usage....:\033\[0m \033\[36mYour disk is ${usage} full\033\[0m"
puts "  \033\[35mSSH Logins....:\033\[0m \033\[36mThere are currently $log(c) users logged in\033\[0m"
puts "  \033\[35mProcesses.....:\033\[0m \033\[36m$psa total running of which $psu $verb yours\033\[0m"
#puts "  \033\[35mWeather.......:\033\[0m \033\[36m$weather $wthr(t) $wthr(d) $wthr(e)\n\033\[0m"
puts "  \033\[35mWeather.......:\033\[0m \033\[36m$now\033\[0m"
puts "  \033\[35m               \033\[0m \033\[36m$today\033\[0m"
puts "  \033\[35m               \033\[0m \033\[36m$tomorrow\033\[0m"
puts "\n"
puts "\033\[01;32m       ==========================    RULES    =========================="
puts "\033\[01;32m                      --- Posted: No Tresspassing ---"
puts "     This is a private system. You are not to give out access to anyone"
puts "    without permission from the admin/owner. No illegal files or activity."
puts "     Stay in your home directory, do not view/touch/change things that "
puts "    do not belong to you, keep the system clean, and make regular backups."
puts "     -==  DISABLE YOUR PROGRAMS FROM KEEPING SENSITIVE LOGS OR HISTORY ==-\033\[0m\n"

Make /etc/motd.tcl executable

chmod 755 /etc/motd.tcl

Weather

The above script is going to want to show the weather. In order to do so, you will need to add the this weather script to /usr/local/bin/weather-display

#!/bin/sh
# Shows the weather. Save as /usr/local/bin/weather-display

# Fill in form to find your weather code here: 
# http://netweather.accuweather.com/signup-page2.asp
# If code has a space remove it or replace it with %20 or a dash; -
# in the US, use your zip code
LOCCOD="XXXXX"  #Example: NAM|MX|MX009|MEXICO-CITY

# Set how you want to display the temperature.
# 0 for F, 1 for C
METRIC=0

##########

if [ -z $1 ] && [ -x $LOCCOD ] ; then
        echo
        echo "USAGE: $0 [locationcode]"
        echo
        exit 0;
elif [ ! -z $1 ] ; then
LOCCOD=$1
fi

# get today's and tomorrow's:
data="$(curl --max-time 5 -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=$LOCCOD \
| grep description | sed 's/<description>//g' | sed 's/^\t*//g' \
| grep -v 'To discover additional' | sed 's/&lt;img.*<\/description>//g' \
| grep -v AccuWeather.com | grep -v '^</description>' | sed 's/ &#176;/°/g' | sed 's/\r//g' \
| sed 's/High://g' | sed 's/ Low:/,/g' | sed 's/ F/°F high/' | sed 's/ F/°F low, and/')"

echo "$(echo -e "$data" | sed 's/ in .*, .*: /: /g' | head -n 1)" # \(in $LOCCOD\)
echo "    Today:$(echo "$data" | head -n 2 | tail -n 1)"
echo " Tomorrow:$(echo "$data" | tail -n 1)"

You will need to change LOCCOD="XXXXX" to your zipcode or city. And set if you use Metric or not. Then make it executable.

chmod +x /usr/local/bin/weather-display

Test it out

Now, make sure you have tcl/tclsh installed. The simplest way is just to run the program and see if it works:

/etc/motd.tcl

You should see something like this:

image-center

Put it to work

To use the script, add it to the system bash/sh profile by saving the following as /etc/profile.d/motd.sh. Or you can add this to your .bash_profile.

# Check for interactive bash.
# Save as /etc/profile.d/motd.sh
if [ -n "$BASH_VERSION" ] && [ -n "$PS1" ] ; then
        /etc/motd.tcl
fi

Finally, log in (or open another terminal) and see the magic.

And if you use a different shell, just add the line /etc/motd.tcl to its profile file. And I’d suggest not letting it run on non-interactive shells, as that sometimes can cause issues. See the shell’s docs on how to do that.

Customize

The great thing about this is you can customize it to your liking. There’s several lines in motd.tcl that are commented out. So feel free to add things like waiting system updates, or checks for system daemons, or random quotes from fortune:

echo -e "\e[00;35m$(fortune | cowsay -f small)\e[00m" 

But you’d usually just add that to your .bash_profile.

Do NOT set the suid bit on /etc/motd.tcl nor /etc/profile.d/motd.sh. If your customization needs to have a program run as root, use sudo with the full command line and all arguments. Otherwise you may open your system to attacks.

And while you’re customizing the login, be sure to check out my other article about how to show a notice that warns others before they try to login.

Sources

Scripts and instructions originally from Mewbies.

Other ways to change the font.