Get system stats automatically on SSH login

I noticed in recent versions of Ubuntu you get some system stats in a banner message when you connect using SSH. I thought this was pretty useful so have implemented my own version on our Red Hat servers at work.

This runs every 5 minutes as a cron job and updates the file /etc/motd (Message Of The Day) which is shown when a user logs in. Our servers already have a 5 line banner message with information including the server name and purpose of the server, hence the first 5 lines being saved and readded into a new /etc/motd each time.

#!/bin/bash cat /etc/motd | head -n 5 > /tmp/file && cat /tmp/file > /etc/motd CPUTIME=$(ps -eo pcpu | awk 'NR>1' | awk '{tot=tot+$1} END {print tot}') CPUCORES=$(cat /proc/cpuinfo | grep -c processor) echo "System summary (collected `date`) - CPU Usage (total average) = `echo $CPUTIME / $CPUCORES | bc`% - Memory free (real) = `free -m | head -n 2 | tail -n 1 | awk {'print $4'}` Mb - Memory free (cache) = `free -m | head -n 3 | tail -n 1 | awk {'print $3'}` Mb - Swap in use = `free -m | tail -n 1 | awk {'print $3'}` Mb " >> /etc/motd

Now when we log in we get a summary like this:

SSH Server Stats

0 Responses to “Get system stats automatically on SSH login”


  1. No Comments

Leave a Reply