Created
September 29, 2010 02:55
-
-
Save elecnix/602230 to your computer and use it in GitHub Desktop.
Send by e-mail new entries in the Bash history of all users.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Send by e-mail new entries in the Bash history of all users. | |
# | |
# usage: mail-history <email> <subject> | |
# | |
TMPFILE=`tempfile 2>/dev/null` || TMPFILE=`mktemp` || exit 1 | |
declare -i CHANGES | |
CHANGES=0 | |
TO=$1 | |
SUBJECT=$2 | |
function append_history { | |
USERNAME=$1 | |
HOMEDIR=$2 | |
SEP="################################################" | |
if [ -r $HOMEDIR/.bash_history ] ; then | |
if [ -r $HOMEDIR/.bash_history.mailed ] ; then | |
diff $HOMEDIR/.bash_history.mailed $HOMEDIR/.bash_history > /dev/null || { | |
( echo && echo $SEP && echo "new commands in bash history of $USERNAME: " && echo ) >> $TMPFILE | |
( diff $HOMEDIR/.bash_history.mailed $HOMEDIR/.bash_history | grep -e '^>' | sed -e 's/> //' ) >> $TMPFILE | |
CHANGES=$CHANGES+1 | |
} | |
else | |
( echo && echo $SEP && echo "new commands in bash history of $USERNAME: " && echo && cat $HOMEDIR/.bash_history ) >> $TMPFILE | |
CHANGES=$CHANGES+1 | |
fi | |
cp $HOMEDIR/.bash_history $HOMEDIR/.bash_history.mailed | |
fi | |
} | |
( last -a && echo ) >> $TMPFILE | |
append_history root /root | |
for user in `ls /home` ; do | |
append_history $user /home/$user | |
done | |
if [ "x$CHANGES" != "x0" ] ; then | |
cat $TMPFILE | mail -s "$SUBJECT" $TO | |
fi | |
rm $TMPFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment