Files
Server-scripts/utils/profile_motd.sh
T

162 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
[[ -z "$TERM" || "$TERM" == "dumb" ]] && export TERM=xterm-256color
print_line() {
local width
width=$(tput cols 2>/dev/null || echo 80)
printf '%*s\n' "$width" '' | tr ' ' '*'
}
center() {
local width text padding
text="$1"
width=$(tput cols 2>/dev/null || echo 80)
padding=$(( (width - ${#text}) / 2 ))
printf "%*s%s\n" "$padding" "" "$text"
}
tab() {
local target="$1"
local text="$2"
(( $# < 2 )) && {
printf '\t'
return
}
printf "%*s" $(( target - ${#text} > 0 ? target - ${#text} : 1 )) ""
}
#
# Header
#
hour=$(date +%-H)
if (( hour < 12 )); then
greeting="Good Morning"
elif (( hour < 19 )); then
greeting="Good Afternoon"
else
greeting="Good Evening"
fi
print_line
center "Server: $HOSTNAME"
print_line
printf "\n%s %s!\n\n" "$greeting" "$(whoami)"
printf "%s\n" "$(date '+%A, %d %B %Y, %T')"
printf "Uptime: %s\n\n" "$(uptime -p)"
#
# Network
#
mapfile -t ifaces < <(
ip -br link |
awk '$1 != "lo" {print $1}'
)
declare -A iface_ips
for iface in "${ifaces[@]}"; do
iface_ips["$iface"]=$(
ip -4 -o addr show "$iface" 2>/dev/null |
awk '{print $4}' |
cut -d/ -f1 |
head -n1
)
[[ -z "${iface_ips[$iface]}" ]] && iface_ips["$iface"]="----------"
done
PUBLIC_IP=$(curl -fsS --max-time 5 ifconfig.co 2>/dev/null)
COUNTRY=$(curl -fsS --max-time 5 ifconfig.co/country 2>/dev/null)
CITY=$(curl -fsS --max-time 5 ifconfig.co/city 2>/dev/null)
PUBLIC_IP=${PUBLIC_IP:-"IP unavailable"}
COUNTRY=${COUNTRY:-"Unknown"}
CITY=${CITY:-"Unknown"}
printf "Network:\n"
# Physical interfaces
for iface in "${ifaces[@]}"; do
[[ "$iface" =~ ^(lo|docker0|br-|veth) ]] && continue
ip="${iface_ips[$iface]}"
[[ "$ip" == "----------" ]] && continue
printf " %-6s %-15s (%s)\n" \
"LAN" \
"$ip" \
"$iface"
done
printf " %-6s %-15s (%s, %s)\n" \
"WAN" \
"$PUBLIC_IP" \
"$CITY" \
"$COUNTRY"
printf " %-6s %s\n" \
"User" \
"$(whoami)"
# Docker networks
docker_found=false
for iface in "${ifaces[@]}"; do
if [[ "$iface" =~ ^(docker0|br-) ]]; then
if ! $docker_found; then
printf "\nDocker:\n"
docker_found=true
fi
printf " %-12s %s\n" \
"${iface:0:12}" \
"${iface_ips[$iface]}"
fi
done
printf "\n"
#
# Memory
#
read -r mem_total mem_used mem_free <<< "$(
free -h |
awk 'NR==2 {print $2, $3, $7}'
)"
printf "%-10s %8s total %8s used %8s free\n" \
"Memory:" "$mem_total" "$mem_used" "$mem_free"
#
# Storage
#
while read -r filesystem size used avail pct mountpoint; do
case "$mountpoint" in
*efi*|*win10*)
continue
;;
/)
label="/root:"
;;
*)
label="/$(basename "$mountpoint"):"
;;
esac
printf "%-10s %8s total %8s used %8s free (%4s)\n" \
"$label" "$size" "$used" "$avail" "$pct"
done < <(
df -h --output=source,size,used,avail,pcent,target |
awk 'NR>1 && $1 ~ /^\/dev\//'
)
printf "\n"