Skip to content

Instantly share code, notes, and snippets.

@arvati
Forked from ergoz/motd_generator.sh
Last active June 5, 2024 05:46
Show Gist options
  • Save arvati/7d698d4472c8b2a6a9995b05f168c15a to your computer and use it in GitHub Desktop.
Save arvati/7d698d4472c8b2a6a9995b05f168c15a to your computer and use it in GitHub Desktop.
Dynamic motd generator for Alpine Linux (/etc/periodic/15min/motd)

Make a dynamic motd for your server

create a crond script to dynamic create an motd message to users

rc-service crond start && rc-update add crond
nano /etc/periodic/15min/motd
chmod a+x /etc/periodic/15min/motd
run-parts --test /etc/periodic/15min

Contents of /etc/periodic/15min/motd

#!/bin/sh
#. /etc/os-release
PRETTY_NAME=`awk -F= '$1=="PRETTY_NAME" { print $2 ;}' /etc/os-release | tr -d '"'`
VERSION_ID=`awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release`
UPTIME_DAYS=$(expr `cat /proc/uptime | cut -d '.' -f1` % 31556926 / 86400)
UPTIME_HOURS=$(expr `cat /proc/uptime | cut -d '.' -f1` % 31556926 % 86400 / 3600)
UPTIME_MINUTES=$(expr `cat /proc/uptime | cut -d '.' -f1` % 31556926 % 86400 % 3600 / 60)
cat > /etc/motd << EOF
%+++++++++++++++++++++++++++++++ SERVER INFO ++++++++++++++++++++++++++++++++%
%                                                                            %
        Name: `hostname`
        Uptime: $UPTIME_DAYS days, $UPTIME_HOURS hours, $UPTIME_MINUTES minutes
        CPU: `cat /proc/cpuinfo | grep 'model name' | head  -1 | cut -d':' -f2`
        Memory: `free -m | head -n 2 | tail -n 1 | awk {'print  $2'}`M
        Swap: `free -m | tail -n 1 | awk {'print $2'}`M Disk: `df -h / | awk  '{ a = $2 } END { print a }'`

        Kernel: `uname -r`
        Distro: $PRETTY_NAME
        Version $VERSION_ID

        CPU Load: `cat /proc/loadavg | awk '{print $1 ", " $2 ", " $3}'`
        Free Memory: `free -m | head -n 2 | tail -n 1 | awk {'print $4'}`M
        Free Swap: `free -m | tail -n 1 | awk {'print $4'}`M
        Free Disk: `df -h / | awk '{ a =  $2 } END { print a }'`

        eth0 Address: `ifconfig eth0 | grep "inet addr" |  awk -F: '{print $2}' | awk '{print $1}'`
%                                                                            %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
EOF

This is the result:
cat /etc/motd

%+++++++++++++++++++++++++++++++ SERVER INFO ++++++++++++++++++++++++++++++++%
%                                                                            %
        Name: vcs
        Uptime: 8 days, 18 hours, 45 minutes
        CPU:  Intel(R) Atom(TM) CPU D525   @ 1.80GHz
        Memory: 3938M
        Swap: 1023M Disk: 5.5T

        Kernel: 4.14.63
        Distro: Alpine Linux edge
        Version 3.12_alpha20200428

        CPU Load: 0.06, 0.02, 0.00
        Free Memory: 2313M
        Free Swap: 809M
        Free Disk: 5.5T

        eth0 Address: 192.168.1.74
%                                                                            %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
@xproot
Copy link

xproot commented Jul 31, 2021

does it work though?

@vivalenta
Copy link

cool, thanks

@xpacio
Copy link

xpacio commented Jul 13, 2023

Just what I was looking for. I adapted it to run with
nano /etc/profile.d/99motd.sh
so it runs only when i log in

thank you arvati, nice script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment