venerdì 5 dicembre 2014

[SNIPPET][bash] extract email addresses from CPanel

This requires that you activated the SSH access via CPanel.

Works well with hostmonster, bluehost and in general with any chrooted environment based on CPanel, where you don't have root password / sudo access. I haven't tried it on WHM based hosting providers.

#!/bin/bash
OWNER=$@
CONTACT=`ls -1A /var/cpanel/users/`

count=1
for x in `echo -n "$CONTACT"`;do
  users=`grep -i ^dns /var/cpanel/users/"$x" |cut -d= -f2`
  DOMAIN[$count]=$users
  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