Ubuntu Dynamic MOTD

I'm just finishing off moving all my web sites and related things from one VPS host to another. The new host had customized the message of the day on the Ubuntu 12.04 LTS image which I had installed. It had helpful links to support and what not, things that are only actually helpful the first time I logged in.

As I went through the process of removing all this I had to learn about the update-motd system, since I quickly found that the solution wasn't a simple:

$ sudo echo "" > /etc/motd

So I cooked up a bash script to spit out some information about the system every time I log into the server. The output looks like this:

!-- image ubuntu-dynamic-motd_1.png --!

Create or edit update-motd's 00-header file.

$ sudo vi /etc/update-motd.d/00-header

And here's the source of the bash script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh

[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then  
 # Fall back to using the very slow lsb_release utility  
 DISTRIB_DESCRIPTION=$(lsb_release -s -d)  
 fi

UPTIME=`uptime | awk '{if ($4 == "day," || $4 == "days,") print $3, $4, $5; else print $3}' | awk -F: '{print $1, "hrs", $2, "mins"}' | sed 's/,//g'`

LOADAVG=`uptime | awk '{if ($4 == "day," || $4 == "days,") print $10, $11, $12; else print $8, $9, $10}'`

PROCCOUNT=`ps -l | wc -l`  
 PROCCOUNT=`expr $PROCCOUNT - 4`  
 IP=$(/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')

echo -e "  
 ==============: System Info :===================  
 Hostname: `hostname`  
 Address: $IP  
 Distro: $DISTRIB_DESCRIPTION  
 Kernel: `uname -r`  
 Uptime: $UPTIME  
 Load Avgs: $LOADAVG  
 Processes: $PROCCOUNT  
 ==============: Memory Info :===================  
 Total: `cat /proc/meminfo | grep MemTotal | awk {'print $2'}` kB  
 Free: `cat /proc/meminfo | grep MemFree | awk {'print $2'}` kB  
 Lowest: `cat /proc/meminfo | grep LowFree | awk {'print $2'}` kB  
 ===============: Disk Info :===================="  
 df -h
Contents © 2017