Mini Shell
#!/bin/bash
######################################################################################################
# Script: mailscan
# Author: Jason N
# Version: 1.0.2
# Contact: jasonn@inmotionhosting.com
# Purpose: Scans all account mail inboxes, sent boxes, and recent login usage and reports account size, message counts, and logins per protocol
######################################################################################################
# Version 1.0.2 - minor bugfix / removal of extra code
# variables
userpath=/var/cpanel/users
username=$1
maildomains=
logcount=300000
inbox_limit=0
#functions
function usagetxt {
if [[ $errortxt != "" && "$helptxt" != "1" ]] ; then
echo -e "\nERROR: $errortxt";
fi
echo -e "\nUsage: mailscan <username> <options>";
if [[ "$helptxt" -eq "1" ]] ; then
echo -e "\n\tAvailable options:";
echo -e "\t\t-s / --summary\t\t: Only output account, size, and inbox count fields";
# echo -e "\t\t-c / --current\t\t: Get mail account list from current process list - in development";
# echo -e "\t\t-ac / --all-current\t: Get all current mail connections to the system - in development";
echo -e "\t\t-lc= / --log-count=\t: Change the default line history mailscan will use when reviewing /var/log/maillog (default is $logcount)";
echo -e "\t\t-il= / --inbox-limit=\t: Change the default inbox message count that will be displayed (default is $inbox_limit)\n";
else
echo -e "\n\tHelp available with option -h or --help";
fi
exit;
}
#parse command options
for i in "$@" ; do
case $i in
-s | --summary) summary="1" ;;
-ac | --all-current) all_current="1" ;;
-c | --current) current_conns="1" ;;
-lc* | --log-count*) logcount=`echo $i | awk -F'=' '{gsub(/ /,"",$2); print $2}'` ;;
-il* | --inbox-limit*) inbox_limit=`echo $i | awk -F'=' '{gsub(/ /,"",$2); print $2}'` ;;
-h | --help) errortxt="Help page."; helptxt="1" ; usagetxt ;;
esac
done
# get list of local home directories from system
homedirs=`ls / | sed -e 's/\///g' | egrep "^home$|^home[0-9]$"`
if [[ "$#" -eq "0" ]] ; then
errortxt="No user provided";
usagetxt
exit
fi
#parse_options
if [[ "$all_current" -eq "1" ]] ; then
echo "Under Development";
exit
elif [[ -e $userpath/$username ]]
then
echo "User $username found, scanning"
else
errortxt="User $username does not exist on this system";
usagetxt
exit
fi
# find users home directory and verify it exists
for tmphomedir in $homedirs ; do
if [[ -e /$tmphomedir/$username ]] ; then
homedir=$tmphomedir
fi
done
# get the list of mail domains on the account from etc in their home directory
maildomains=`ls /$homedir/$username/etc| egrep -v "\.\/|\.\.\/|\.boxtrapper|ftpquota|rcube.db|passwd\,v|quota\,v|shadow\,v|\.imapv4"`
#loop through list of mail domains and get email account details
for currentdomain in $maildomains ;
do
scandomain=/$homedir/$username/mail/$currentdomain ; # path to domain to scan
if [[ -e "$scandomain" ]] ; then
cd $scandomain ;
workingdomain=`pwd|awk -F'/' '{print $5}'`;
# loop through the list of users in the domains mail directory
for i in $(ls| egrep -v "\.\/|\.\.\/"| awk -F'/' '{print $1}') ;
do
address="$i@$workingdomain"; # email address
if [[ ! -e $i/cur ]] ; then
continue;
fi
dsize=`du -sh $i/cur | awk '{print $1}'` ; # size of mail inbox
inboxcount=`ls $i/cur | wc -l`; # number of messages in inbox
if [[ -e $i/.Sent/cur ]] ; then
sentcount=`ls $i/.Sent/cur | wc -l`; # number of sent messages
else
sentcount=0;
fi
# get number of recent imap logins
imapcount=`tail -n$logcount /var/log/maillog | grep $address | egrep "imapd:|imapd-ssl:" | grep LOGIN | wc -l`;
# get number of recent pop logins
popcount=`tail -n$logcount /var/log/maillog | grep $address | egrep "pop3d:|pop3d-ssl:" | grep LOGIN | wc -l`;
# output the results
if [[ "$summary" -eq "1" ]] ; then
echo $address","$dsize","$inboxcount","$inbox_limit | awk -F',' '$3 >= $4 {printf "%40s\t Inbox Size: %6s\tInbox Count: %6s\n",$1, $2, $3}';
else
echo $address","$dsize","$inboxcount","$sentcount","$imapcount","$popcount","$inbox_limit | awk -F',' '$3 >= $7 {printf "%40s\t Inbox Size: %6s\tInbox: %6s\tSent:%6s\tIMAP: %4s\tPOP: %4s\n",$1, $2, $3, $4, $5, $6}';
fi
done;
fi
done;
Zerion Mini Shell 1.0