cPanel - Bash script to List All eMail Accounts using command line


  List All eMail Accounts in cPanel Server using command line


The bash script given below will list all the mail accounts in a cpanel server and the list is saved in '/root/mailaccounts.txt'. Depending upon the total accounts, it will take time to finish script.


#!/bin/bash -  
#title          :listallmailaccounts.sh
#description    :To List All Mail Accounts in cPanel via Terminal
#author         :Arun Ghosh
#date           :20130811
#version        :1.0   
#usage          :./listallmailaccounts.sh
#notes          :      
#bash_version   :4.2.25(1)-release
#============================================================================

cat /dev/null > /root/mailaccounts.txt

CP_ACCOUNTS=`ls -1A /var/cpanel/users/`

for user in `echo -n $CP_ACCOUNTS`
do/root/mailaccounts.txt
    domain=`grep -i ^dns /var/cpanel/users/$user |cut -d= -f2`
    for dom in `echo -n "$domain"`
    do
        PASSWD_FILE="/home/$user/etc/$dom/passwd"
        if [ -f $PASSWD_FILE ] && [ -s $PASSWD_FILE ]
        then
            for mail in `cat $PASSWD_FILE| cut -d":" -f1`
            do
                echo "$mail@$dom" >> mailaccounts.txt
            done
        fi
    done
done

One thought on “cPanel - Bash script to List All eMail Accounts using command line”

  1. Need to delete the random "/root/mailaccounts.txt" on line... 17?
    But besides that, worked, awesome