Dynamic Message of the Day - motd - Fedora Linux

HOW-TO: Dynamic Message of the day

This article is  about setting up a dynamic message of the day (possibly informative and fun) as header of each newly opened shell.

The final result will be something like the following:

header_shell

It mixes a fun message using fortune and cowsay which you can install using


sudo dnf install fortune-mod cowsay

utility and some informative info about the status of the system as:

  • System load
  • Ram and Swap available and used
  • Disk space
  • Ip address

 

The script file can be easily configured and extended to suits the your needs. Colors can also be easily customized.

#!/bin/bash

# Installation:
#
# 1.   vim /etc/ssh/sshd_config 
#      PrintMotd no
# 2. update your bachrc.sh
#      echo "pathTothisFile" >> ~/.bashrc.sh
#
# !!requires fortune cowsay, lm_sensors!!

USER=`whoami`
HOSTNAME=`uname -n`
IP=`hostname -i`
ROOT=`df -Ph | grep root | awk '{print $4}' | tr -d '\n'`
HOME=`df -Ph | grep home | awk '{print $4}' | tr -d '\n'`
BACKUP=`df -Ph | grep backup | awk '{print $4}' | tr -d '\n'`
NUM_UNIQUE_USERS=`users | tr ' ' '\n' | sort | uniq | wc -w`
MEMORY=`free -m | grep "Mem" | awk '{print $2,"-",$3,"-",$4}'`
SWAP=`free -m | grep "Swap" | awk '{print $2,"-",$3,"-",$4}'`
PSA=`ps -Afl | wc -l`
TEMPERATURE=`sensors  | grep Core  | awk {'print $3'} | xargs`

#weather
#URL='http://www.accuweather.com/en/gb/edinburgh/eh1-3/weather-forecast/327336'
#WEATHER=`wget -q -O- --timeout=2 "$URL" | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $2": "$16", "$12"°" }'| head -1`


# time of day
HOUR=$(date +"%H")
if [ $HOUR -lt 12  -a $HOUR -ge 0 ]
then    TIME="morning"
elif [ $HOUR -lt 17 -a $HOUR -ge 12 ] 
then    TIME="afternoon"
else 
    TIME="evening"
fi

#System uptime
uptime=`cat /proc/uptime | cut -f1 -d.`
upDays=`expr $uptime / 60 / 60 / 24`
upHours=`expr $uptime / 60 / 60 % 24`
upMins=`expr $uptime / 60 % 60`
upSecs=`expr $uptime % 60`

#System load
LOAD1=`cat /proc/loadavg | awk {'print $1'}`
LOAD5=`cat /proc/loadavg | awk {'print $2'}`
LOAD15=`cat /proc/loadavg | awk {'print $3'}`

COLOR_COLUMN="\e[1m-"
COLOR_VALUE="\e[31m"
RESET_COLORS="\e[0m"

echo ""
echo -e "\e[7m--- GOOD $TIME $USER ----\e[0m"
echo ""
MESSAGE=`/usr/bin/fortune | /usr/bin/cowsay`
echo -e " $MESSAGE"
RULES="$RESET_COLORS\e[32m
  ::::::::::::::::::::::::::::::::::-RULES-::::::::::::::::::::::::::::::::::
    This is a private system that you are not to give out access to anyone   
    without permission from the admin. No illegal files or activity. Stay,   
    in your home directory, keep the system clean, and make regular backups. 
     -==  DISABLE YOUR PROGRAMS FROM KEEPING SENSITIVE LOGS OR HISTORY ==-$RESET_COLORS"
echo -e " $RULES"


echo -e "

 $COLOR_COLUMN- Hostname$RESET_COLORS............: $COLOR_VALUE $HOSTNAME $RESET_COLORS
 $COLOR_COLUMN- IP Address$RESET_COLORS..........: $COLOR_VALUE $IP $RESET_COLORS
 $COLOR_COLUMN- Release$RESET_COLORS.............: $COLOR_VALUE `cat /etc/redhat-release` $RESET_COLORS
 $COLOR_COLUMN- Users$RESET_COLORS...............: $COLOR_VALUE Currently $NUM_UNIQUE_USERS DIFFERENT user(s) logged on $RESET_COLORS
 $COLOR_COLUMN- Current user$RESET_COLORS........: $COLOR_VALUE $USER $RESET_COLORS
 $COLOR_COLUMN- CPU usage$RESET_COLORS...........: $COLOR_VALUE $LOAD1 - $LOAD5 - $LOAD15 (1-5-15 min) $RESET_COLORS
 $COLOR_COLUMN- Cores Temperature$RESET_COLORS...: $COLOR_VALUE $TEMPERATURE $RESET_COLORS
 $COLOR_COLUMN- Memory used$RESET_COLORS.........: $COLOR_VALUE $MEMORY (total-free-used) $RESET_COLORS
 $COLOR_COLUMN- Swap in use$RESET_COLORS.........: $COLOR_VALUE $SWAP (total-used-free) MB $RESET_COLORS
 $COLOR_COLUMN- Processes$RESET_COLORS...........: $COLOR_VALUE $PSA running $RESET_COLORS
 $COLOR_COLUMN- System uptime$RESET_COLORS.......: $COLOR_VALUE $upDays days $upHours hours $upMins minutes $upSecs seconds $RESET_COLORS
 $COLOR_COLUMN- Disk space HOME$RESET_COLORS.....: $COLOR_VALUE $HOME remaining $RESET_COLORS
"


here a github link

Enjoy!

2 Comments

  • I'm on Fedora 24/25 and I had to use the .bashrc not .bashrc.sh and in my custom motd script I had to reference it this way: sh custom-motd.

    Also how can the weather be customized? Why is it commented out?
    #weather
    #URL='http://www.accuweather.com/en/gb/edinburgh/eh1-3/weather-forecast/327336'
    #WEATHER=`wget -q -O- --timeout=2 "$URL" | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $2": "$16", "$12"°" }'| head -1`

    • Hi, the weather part is commented out because there will be a little delay every time a new terminal is open.
      I'm sure it's not hard to script it in such a way the webpage would be downloaded once every couple of hours.
      Let me know if you code it!

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>