Skip to content

Instantly share code, notes, and snippets.

@tetafro
Last active September 8, 2016 10:15
Show Gist options
  • Save tetafro/96992f2f00e083b8690777a37d8e5234 to your computer and use it in GitHub Desktop.
Save tetafro/96992f2f00e083b8690777a37d8e5234 to your computer and use it in GitHub Desktop.
Email notification about logged in user
#!/bin/bash
# Put this script in /etc/profile for autorun.
num=1
# Search login record in /var/log/auth.log down up
while :
do
# Next line
string=$(tail -n $num /var/log/auth.log | head -n 1)
# 6th word (it should be "Accepted")
auth=$(echo $string | awk '{print $6}')
if [ "$auth" == 'Accepted' ]
then
user=$(echo $string | awk '{print $9}')
address=$(echo $string | awk '{print $11}')
month=$(echo $string | awk '{print $1}')
day=$(echo $string | awk '{print $2}')
time=$(echo $string | awk '{print $3}')
# Send to admin by email
echo "$user logged in from $address at $time ($month $day)" | mailx -s "router login $(date '+%d.%m')" [email protected]
break
fi
# Go to next line
num=$((num + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment