Today I had a customer that has a dedicated server, and wanted a list of every email account, on every domain on his server. He has well over 600 domains on the server, and at first I thought this would take forever. However, I thought about it, and came up with this solution
Create a file called email-list.sh
Set the permissions on the file to 700 and run it as root
Here’s the script
#!/bin/bash
OWNER=$@
KONTA=`ls -1A /var/cpanel/users/`
count=1
for x in `echo -n “$KONTA”`;do
wiersz=`grep -i ^dns /var/cpanel/users/”$x” |cut -d= -f2`
DOMAIN[$count]=$wiersz
count=$[$count+1]
echo “Login: `echo “$x”`”
for i in `echo “${DOMAIN[@]}” | sed ‘s/ /\n/g’`;do
for n in ` ls -A /home/”$x”/mail/”$i”/ 2>/dev/null`;do
if [ “$n” == “cur” ];then echo “$n” > /dev/null
elif [ “$n” == “new” ];then echo “$n” > /dev/null
elif [ “$n” == “tmp” ];then echo “$n” > /dev/null
elif [ “$n” == “” ];then echo “$n” > /dev/null
else
echo “$n”@”$i”
fi
done
done
echo;echo;
done
This will create a list of all of the email accounts on the server and what username they’re associated with. We place the script in /root and run it as:
/root/email-lists.sh
If you want to save the results to a text file you can pipe the results easily by running:
/root/email-lists.sh >> /root/email-list.txt
Then all of the email account data is saved in the file /root/email-list.txt and you can view the file. To view the file I prefer to use cat:
cat /root/email-lists.txt