Skip to content

Instantly share code, notes, and snippets.

@mfgbhatti
Last active February 11, 2024 05:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mfgbhatti/2c2ea5da5d6d7e608d90fd43d4daa4a8 to your computer and use it in GitHub Desktop.
Save mfgbhatti/2c2ea5da5d6d7e608d90fd43d4daa4a8 to your computer and use it in GitHub Desktop.
Ubuntu like motd for arch linux systems
#!/usr/bin/env bash
# ____ __ __ __ __ _
# ____ ___ / __/___ _/ /_ / /_ ____ _/ /_/ /_(_)
# / __ `__ \/ /_/ __ `/ __ \/ __ \/ __ `/ __/ __/ /
# / / / / / / __/ /_/ / /_/ / / / / /_/ / /_/ /_/ /
#/_/ /_/ /_/_/ \__, /_.___/_/ /_/\__,_/\__/\__/_/
# /____/
#
# https://gist.github.com/mfgbhatti/2c2ea5da5d6d7e608d90fd43d4daa4a8
# This script provide ubuntu like MOTDS on Arch Linux servers.
# This MOTD uses the checkupdates command to obtain the list of upgradeables packages
# so it is necessary to install. After kernel update show you restart your system to apply kernel.
# Show system IPs (may be you have to tinker the a bit)
# Show memory usage
# CPU load
# user signed in
# system up time
# and more
## Installation
# copy this to /usr/bin/update_motd.sh
# make it executable (chmod +x)
# edit /etc/pam.d/sshd and add
# session optional pam_exec.so stdout /usr/bin/update_motd.sh
# Make sure this will be trigger when you ssh to your server
# Enjoy
motd() {
# Variables
MOTD_DATE="$(date)"
MOTD_MEMORY="$(free -m | awk '/Mem/ { printf("%3.1f%%", $3/$2*100) }')"
MOTD_SWAP="$(free -m | awk '/Swap/ { printf("%3.1f%%", $3/$2*100) }')"
# MOTD_ROOT_FULL="$(df -h / | awk '/\// {print $(NF-2)}')"
# MOTD_ROOT_USED="$(df -h / | awk '/\// {print $(NF-1)}')"
MOTD_ROOT_FULL=" $(df -l --total -h | awk -F' ' '/total/{print $2}')B"
MOTD_ROOT_USED="$(df -l --total -h | awk -F' ' '/total/{print $3}')B"
MOTD_ROOT_PERCENT="$(df -l --total -h | awk -F' ' '/total/{print $5}')"
MOTD_CPU_LOAD="$(awk '{print $1}' /proc/loadavg)"
MOTD_UPDATES="$(checkupdates | wc -l)"
MOTD_USERS="$(users | wc -w)"
MOTD_PROCS="$(ps aux | wc -l)"
MOTD_DOCKER="$(ip addr show | grep -v inet6 | grep inet | awk 'NR==4 {print $2}' | cut -d/ -f 1)"
# MOTD_IP4="$(ip addr show | grep -v inet6 | grep inet | awk 'NR==2 {print $2}' | cut -d/ -f 1)"
# MOTD_IP4="$(ip addr show | awk '/eth0/ {print $2}' | tail -1 | cut -d/ -f1)"
MOTD_IP6="$(ip addr show | grep inet6 | awk 'NR==2 {print $2}' | cut -d/ -f1)"
MOTD_IP4="$(ip addr show | awk '/eth0/ {print $2}' | sed -n '2p;' | sed 's!/.*!!')"
# System uptime
UPTIME="$( cut -f1 -d. </proc/uptime)"
UPDAYS="$(( UPTIME/60/60/24 ))"
UPHOURS="$(( UPTIME/60/60%24 ))"
UPMINS="$(( UPTIME/60%60 ))"
UPSECS="$(( UPTIME%60 ))"
# Statements
if [[ "$UPHOURS" -lt "10" ]]; then
UPHOURS="0$UPHOURS"
fi
if [[ "$UPMINS" -lt "10" ]]; then
UPMINS="0$UPMINS"
fi
if [[ "$UPSECS" -lt "10" ]]; then
UPSECS="0$UPSECS"
fi
# Check for kernels
if pacman -Q linux > /dev/null 2>&1; then
NEW_KERNEL="$(pacman -Q linux | cut -d " " -f 2 | sed 's/.ar.*$//')" # for linux kernel
OLD_KERNEL="$(uname -r | sed 's/-ar.*$//')"
else
NEW_KERNEL="$(pacman -Q linux-lts | cut -d " " -f 2 | sed 's/.ar.*$//')" # for linux-lts kernel
OLD_KERNEL="$(uname -r | cut -d "-" -f 1,2)"
fi
# Outputs
printf "Welcome to %s (%s)\n" "Arch Linux" "$(uname -r)"
printf "\n"
printf "\t* Documentation:\thttps://wiki.archlinux.org/\n"
printf "\t* Management:\t\thttps://wiki.archlinux.org/title/List_of_applications\n"
printf "\t* Support:\t\thttps://wiki.archlinux.org/title/Frequently_asked_questions\n"
printf "\t* Base:\t\t\thttps://wiki.archlinux.org/title/XDG_Base_Directory\n"
printf "\n"
printf "System information as of %s\n" "$MOTD_DATE"
printf "\n"
printf "\tSystem Uptime:\t\t%s Days and %s:%s:%s\n" "$UPDAYS" "$UPHOURS" "$UPMINS" "$UPSECS"
printf "\tSystem load:\t\t%s\n" "$MOTD_CPU_LOAD"
printf "\tUsage of /:\t\t%s / %s (%s)\n" "$MOTD_ROOT_USED" "$MOTD_ROOT_FULL" "$MOTD_ROOT_PERCENT"
printf "\tMemory usage:\t\t%s\n" "$MOTD_MEMORY"
printf "\tSwap usage:\t\t%s\n" "$MOTD_SWAP"
printf "\tProcesses:\t\t%s\n" "$MOTD_PROCS"
printf "\tUsers logged in:\t%s\n" "$MOTD_USERS"
printf "\tIP address for docker:\t%s\n" "$MOTD_DOCKER"
printf "\tIPv4 address for eth0:\t%s\n" "$MOTD_IP4"
printf "\tIPv6 address for eth0:\t%s\n" "$MOTD_IP6"
printf "\n"
printf "%s updates can be applied immediately.\n" "$MOTD_UPDATES"
[[ "$NEW_KERNEL" > "$OLD_KERNEL" ]] && printf "\n\n*** System restart required ***\n"
}
motd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment