File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ # Get the email addresses of users who have logged in with SSH since a specified number of days ago.
5+
6+ if [ " $# " -eq 0 ]; then
7+ DAYS=30
8+ elif [ " $# " -eq 1 ]; then
9+ DAYS=$1
10+ else
11+ echo " Wrong number of arguments. Usage: $0 [days]"
12+ echo " The optional argument \" days\" specifies how many days back to look for user logins."
13+ fi
14+
15+ DATE=$( date --date " -${DAYS} days" -I)
16+ echo " Looking for users who have logged in with SSH since ${DATE} . This requires sudo access."
17+ USERS=$( sudo journalctl -u ssh --since " ${DATE} " | grep " Accepted" | awk ' {print $9}' | sort --unique)
18+
19+ echo " Searching the LDAP directory for email addresses of the users."
20+ EMAILS=$( for USERNAME in $USERS ; do
21+ ldapsearch " (cn=${USERNAME} )" mail | awk ' /^mail:/ {print $2}'
22+ done | sort --unique)
23+
24+ echo " Email addresses of users who logged in since ${DATE} :"
25+ echo " ${EMAILS} "
You can’t perform that action at this time.
0 commit comments