Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Forked from arvati/alpine_motd_generator.md
Last active September 22, 2024 00:47
Show Gist options
  • Save pythoninthegrass/d2cc2ef4ba6d5291f3f6d0c51fc3e099 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/d2cc2ef4ba6d5291f3f6d0c51fc3e099 to your computer and use it in GitHub Desktop.
Dynamic motd generator for Alpine Linux
#!/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

Custom MOTD on Alpine

This assumes sudo and bash are installed and bash has been set to the default shell.

# download gist
git clone https://gist.github.com/d2cc2ef4ba6d5291f3f6d0c51fc3e099.git motd
cd motd

# copy to /etc
sudo cp 99-motd.sh /etc/

# ~/.bashrc
# Display MOTD on interactive login shells
if [ -n "$PS1" ] && [ -f /etc/99-motd.sh ]; then
    . /etc/99-motd.sh
fi

# ~/.profile
. .bashrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment