Skip to content

Instantly share code, notes, and snippets.

@ppmathis
Last active December 27, 2017 01:06
Show Gist options
  • Save ppmathis/7436447 to your computer and use it in GitHub Desktop.
Save ppmathis/7436447 to your computer and use it in GitHub Desktop.
DynMOTD
#!/bin/sh
# Only for empty SSH subsystems
if [ ! -z "$SSH_ORIGINAL_COMMAND" ]; then
exit 0
fi
# Helper function for centering a toilet banner
center() {
LINE_STR=$1
PADDING_STR=$(printf '%*s' $PADDING '')
echo "$PADDING_STR$LINE_STR"
}
# Centers a text (based on the given screen width)
centerex() {
printf "%*s\n" $(((${#1}+$SCREEN_WIDTH)/2)) "$1"
}
# Create temporary script
cat << 'EOF' > /tmp/strip-chars.pl
#!/usr/bin/perl
while (<>) {
s/ \e[ #%()*+\-.\/]. |
\r | # Remove extra carriage returns also
(?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
(?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
(?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
\e.|[\x80-\x9f] //xg;
1 while s/[^\b][\b]//g; # remove all non-backspace followed by backspace
print;
}
EOF
chmod +x /tmp/strip-chars.pl
# Some variables
SCREEN_WIDTH=$(tput cols)
HOSTNAME=$(hostname -s)
BANNER=$(echo $HOSTNAME | toilet -w $SCREEN_WIDTH --gay -F crop -f broadway -E utf8)
BANNER_WITH_BORDER=$(echo $HOSTNAME | toilet -w 120 --gay -F crop -F border -f broadway -E utf8)
BANNER_WIDTH=$(printf $BANNER_WITH_BORDER | head -n 1 | /tmp/strip-chars.pl | wc -c)
PADDING=$((($SCREEN_WIDTH - $BANNER_WIDTH + 2) / 2))
# Delete temporary script
rm /tmp/strip-chars.pl
# Centered banner created by TOIlet
printf '\n'
echo "$BANNER" | while IFS= read -r line; do
center "$line"
done
# Legal disclaimer
printf '\n'
centerex 'Unauthorized access to this system is prohibited!'
printf '\n'
centerex 'This system is actively monitored and all connections may be logged.'
centerex 'By accessing this system, you consent to this monitoring.'
printf '\n'
# System information
S_HOSTNAME=$(hostname)
S_IP_ADDRESS=$(hostname -I | tr -d ' ')
S_KERNEL=$(uname -sr)
S_CURRENT_USER=$(whoami)
S_SESSION_COUNT=$(who | wc -l)
S_PROCESS_COUNT=$(ps aux | tail -n +2 | wc -l)
S_CORE_COUNT=$(grep -c ^processor /proc/cpuinfo)
S_MEMORY_USAGE=$(free -t -m | grep "Total" | awk '{print $3}')
S_MEMORY_USAGE=$(echo "scale=2; x=$S_MEMORY_USAGE/1000; if(x < 1) print 0; x" | bc)
S_MEMORY_TOTAL=$((($(grep MemTotal /proc/meminfo | awk '{print $2}') + 999999) / 1000000))
centerex "Welcome $S_CURRENT_USER! $S_SESSION_COUNT sessions are currently active."
centerex "You are logged in at $S_HOSTNAME ($S_IP_ADDRESS) running $S_KERNEL."
centerex "There are $S_PROCESS_COUNT processes active, consuming $S_MEMORY_USAGE GiB of memory."
centerex "You can use up to $S_CORE_COUNT cpu cores and $S_MEMORY_TOTAL GiB of memory."
printf '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment