#!/bin/bash
#https://linuxpatch.com/howtos/ubuntu-motd-generator

apt update && apt install -y jc jq
cat <<'EOF' > /etc/update-motd.d/11-its
#!/bin/bash

# System Information
echo "System Information:"
echo "- System Version: $(lsb_release -ds 2>/dev/null)"
echo "- Kernel Version: $(uname -r)"
echo "- CPU Model: $(lscpu | grep 'Model name' | sed 's/Model name:\s*//' | head -n1)"
echo "- CPU Cores: $(grep vendor_id /proc/cpuinfo|wc -l)"
echo "- Total Memory: $(free -h | awk '/^Mem:/ { print $2 }')"
echo "- Used Memory: $(free -h | awk '/^Mem:/ { print $3 }')"
echo "- Available Memory: $(free -h | awk '/^Mem:/ { print $7 }')"
echo "- Disk Usage: $(df -h / | awk 'NR==2 {print $3 " used of " $2 " (" $5 " used)"}')"
echo "- Uptime: $(uptime -p | sed 's/up //')"
echo "- Logged in Users: $(who | wc -l)"
echo "- Last Login: $(last -n 1 -s -1days | awk 'NR==1 {print $1 " on " $2 " " $3 " " $4}')"
echo
echo *********************************INTERFACES*********************************
ifconfig | jc --ifconfig | jq '[.[] | select( .ipv4_addr != null )|{name, IP: .ipv4[].address, MAC: .mac_addr, rx_errors, rx_dropped, tx_errors, tx_dropped}]' | 
	jq -r '["Interface","IP","MAC","RX_err","RX_drop","TX_err","TX_drop"], (.[] | [.name, .IP, .MAC, .rx_errors, .rx_dropped, .tx_errors, .tx_dropped]) | @tsv' | column -t
#ip addr show|grep brd|egrep '^ *inet'|awk -- '{ print $7"\t"$2;}'|sort|column -t
#ip addr show|grep -v brd|egrep '^ *inet'|awk -- '{ print $5"\t"$2;}'|column -t
#echo ************************INTERFACES ERRORS************************
#ifconfig -s | grep -v available | awk -- '{ print $1"\t"$4"\t"$5"\t"$8"\t"$9;}'|column -t
echo
EOF

chmod +x /etc/update-motd.d/11-its
mv /etc/motd /etc/motd.bak

yes | cp -i /etc/pam.d/sshd /root/sshd.1
sed -i 's/^session    optional     pam_motd.so noupdate/#session    optional     pam_motd.so noupdate/g' /etc/pam.d/sshd
