Skip to content

Instantly share code, notes, and snippets.

@carlbennett
Last active March 5, 2020 05:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlbennett/0c0b38332be45f7dc5b2 to your computer and use it in GitHub Desktop.
Save carlbennett/0c0b38332be45f7dc5b2 to your computer and use it in GitHub Desktop.
Prints a dynamic message of the day upon login. To install, copy the file to the `/etc/profile.d/` directory.
#!/bin/bash
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
# Only display motd if tty and not sudoing as root
[ "$PS1" ] && [ "$EUID" -ne 0 ] || return 0
# Run the entire function in its own subshell.
#
# The local keyword in functions prevents inheriting values.
# The subshell prevents exporting them.
#
# Technically, local prevents exporting too. Only the vars that
# could be used before initialized need to be declared local to
# prevent the parent env from leaking into it.
(
MOTD_DEFAULT_VALUE="-"
function show_motd() {
local MOTD_AVAILABILITY_ZONE \
MOTD_INSTANCE_TYPE \
MOTD_VPC_ID \
MOTD_PRIVATE_IP \
MOTD_PUBLIC_IP
MOTD_METADATA_URL="http://169.254.169.254/latest/meta-data"
# Detect if METADATA is available (works on AWS and OpenStack)
MOTD_INSTANCE_ID="$(curl -s --connect-timeout 1 --max-time 1 ${MOTD_METADATA_URL}/instance-id 2>/dev/null)"
if [ -n "${MOTD_INSTANCE_ID}" ]; then
MOTD_AVAILABILITY_ZONE="$(curl -s ${MOTD_METADATA_URL}/placement/availability-zone 2>/dev/null)"
MOTD_INSTANCE_TYPE="$(curl -s ${MOTD_METADATA_URL}/instance-type 2>/dev/null)"
MOTD_INTERFACE_PRIMARY_MAC="$(curl -s ${MOTD_METADATA_URL}/network/interfaces/macs/ 2>/dev/null)"
if [[ "${MOTD_INTERFACE_PRIMARY_MAC}" != *'Not Found'* ]]; then
MOTD_INTERFACE_PRIMARY_MAC="$(echo -n "${MOTD_INTERFACE_PRIMARY_MAC}" | sed -n 1p | cut -c-17)"
MOTD_VPC_ID="$(curl -s ${MOTD_METADATA_URL}/network/interfaces/macs/${MOTD_INTERFACE_PRIMARY_MAC}/vpc-id 2>/dev/null)"
if [[ "${MOTD_VPC_ID}" == *'Not Found'* ]]; then
MOTD_VPC_ID=""
fi
fi
#MOTD_PRIVATE_IP=$(curl -s ${MOTD_METADATA_URL}/local-ipv4/ 2>/dev/null)
#MOTD_PUBLIC_IP="$(curl -s ${MOTD_METADATA_URL}/public-ipv4 2>/dev/null)"
[[ "${MOTD_PUBLIC_IP}" == *'Not Found'* ]] && MOTD_PUBLIC_IP=""
fi
MOTD_OS="$(grep 'PRETTY_NAME' /etc/os-release | cut -d\" -f2 2>/dev/null)"
[ -z "${MOTD_OS}" ] && MOTD_OS="$(cat /etc/system-release | sed 's/ release / /g' 2>/dev/null)"
MOTD_OS_COLOR_B="$(grep 'ANSI_COLOR' /etc/os-release | cut -d\" -f2 2>/dev/null)"
MOTD_HOSTNAME="$(hostnamectl --static 2>/dev/null)"
if [ -z "${MOTD_HOSTNAME}" ]; then
MOTD_HOSTNAME="$(hostnamectl --transient 2>/dev/null)"
if [ -z "${MOTD_HOSTNAME}" ]; then
MOTD_HOSTNAME="$(hostname 2>/dev/null)"
fi
fi
if [ -z "${MOTD_PRIVATE_IP}" ]; then
MOTD_PRIVATE_IP="$(ip -4 -o a sh scope global primary | sed -n 2p | cut -d\ -f7 | cut -d/ -f1 2>/dev/null)"
fi
if [ -z "${MOTD_PUBLIC_IP}" ]; then
MOTD_PUBLIC_IP="$(ip -4 -o a sh scope global primary | sed -n 1p | cut -d\ -f7 | cut -d/ -f1 2>/dev/null)"
fi
MOTD_GATEWAY_IP="$(curl -s -4 http://icanhazip.com/ 2>/dev/null)"
MOTD_TOTAL_CPUS="$(grep processor /proc/cpuinfo | wc -l 2>/dev/null)"
MOTD_TOTAL_DISKS="$(df -h | grep '^\/dev\/' | wc -l 2>/dev/null)"
MOTD_TOTAL_DISK_USED="$(df -h | grep '^\/dev/' | sed -n 1p | awk '{print $3, "/", $2, "(" $5 ")"}' 2>/dev/null)"
MOTD_TOTAL_MEMORY="$(free -h | awk '{print $2}' | sed -n 2p 2>/dev/null)"
if [ "${MOTD_PUBLIC_IP}" = "${MOTD_GATEWAY_IP}" ]; then
MOTD_GATEWAY_IP=""
fi
[ -z "${MOTD_AVAILABILITY_ZONE}" ] && MOTD_AVAILABILITY_ZONE="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_GATEWAY_IP}" ] && MOTD_GATEWAY_IP="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_HOSTNAME}" ] && MOTD_HOSTNAME="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_INSTANCE_ID}" ] && MOTD_INSTANCE_ID="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_INSTANCE_TYPE}" ] && MOTD_INSTANCE_TYPE="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_OS_COLOR_B}" ] && MOTD_OS_COLOR_B="0;32"
[ -z "${MOTD_OS}" ] && MOTD_OS="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_PRIVATE_IP}" ] && MOTD_PRIVATE_IP="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_PUBLIC_IP}" ] && MOTD_PUBLIC_IP="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_TOTAL_CPUS}" ] && MOTD_TOTAL_CPUS="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_TOTAL_DISKS}" ] && MOTD_TOTAL_DISKS="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_TOTAL_DISK_USED}" ] && MOTD_TOTAL_DISK_USED="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_TOTAL_MEMORY}" ] && MOTD_TOTAL_MEMORY="${MOTD_DEFAULT_VALUE}"
[ -z "${MOTD_VPC_ID}" ] && MOTD_VPC_ID="${MOTD_DEFAULT_VALUE}"
MOTD_OS_COLOR_A="1;${MOTD_OS_COLOR_B:2}"
MOTD_PADDING="48" # Arbitrary length
MOTD_PADDING_OS="$((${MOTD_PADDING}-${#MOTD_OS_COLOR_B}-4))" # 4 comes from length of "\033[m"
printf "$(tput sgr0;tput setaf 124)%-$(tput cols)s$(tput sgr0)\n%-$(tput cols)s\n" \
"This system is operated and monitored by a private party." ""
printf " %sHostname: %s\n" "$(tput bold;tput setaf 216)" "$(tput sgr0;tput setaf 180;echo ${MOTD_HOSTNAME})"
printf " %sOS: %-${MOTD_PADDING_OS}s " "$(echo -en "\033[${MOTD_OS_COLOR_A}m")" "$(echo -en "\033[${MOTD_OS_COLOR_B}m${MOTD_OS}")"
printf " %sTotal CPUs: %s\n" "$(tput bold;tput setaf 48)" "$(tput sgr0;tput setaf 78;echo ${MOTD_TOTAL_CPUS})"
printf " %sPublic IP: %-${MOTD_PADDING}s " "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_PUBLIC_IP})"
printf " %sTotal Memory: %s\n" "$(tput bold;tput setaf 48)" "$(tput sgr0;tput setaf 78;echo ${MOTD_TOTAL_MEMORY})"
printf " %sPrivate IP: %-${MOTD_PADDING}s " "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_PRIVATE_IP})"
printf " %sTotal Disks: %s\n" "$(tput bold;tput setaf 48)" "$(tput sgr0;tput setaf 78;echo ${MOTD_TOTAL_DISKS})"
printf " %sGateway IP: %-${MOTD_PADDING}s " "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_GATEWAY_IP})"
printf " %sRoot Vol. Used: %s\n" "$(tput bold;tput setaf 48)" "$(tput sgr0;tput setaf 78;echo ${MOTD_TOTAL_DISK_USED})"
printf "%sInstance Id: %-${MOTD_PADDING}s " "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_INSTANCE_ID})"
printf " %sAvail. Zone: %s\n" "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_AVAILABILITY_ZONE})"
printf " %sVPC Id: %-${MOTD_PADDING}s " "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_VPC_ID})"
printf " %sInstance Type: %s\n" "$(tput bold;tput setaf 250)" "$(tput sgr0;tput setaf 246;echo ${MOTD_INSTANCE_TYPE})"
printf "$(tput sgr0)%-$(tput cols)s\n" ""
}
show_motd || true
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment