Find accounts that do not point to the server – cPanel

Sometimes, it can be useful to remove accounts not pointing to a specific server. Anyway, notice that even if a domain does not point the server, that particular account might be still used. IT can be used for example for a backup location via FTP, or as a test account – and the user is changing the A DNS record/nameservers all the time. Before removing an account make sure to have a backup.

To get a lit of A and nameserver records for a cPanel server:

1. Export the list of the domains to the domains.txt file

# cat /etc/localdomains | sort >> domains.txt

2. Download the script that gets A and nameservers records for a list of domains https://gist.github.com/waako/11559842 We copy/paste the source code here in case it will no longer be available. Save this as domains.sh

#!/bin/bash
# Put all the domain names in domains.txt, one per line
# then run in the terminal: bash domains.sh
# this will print each domain in the terminal as it looks it up
# The result csv will contains the domain, IP & Nameservers in each column

# Give each column the relevant header titles
echo "Domain Name,IP Address,Nameserver,Nameserver,Nameserver,Nameserver,Nameserver" > domains.csv

while read domain
do
  # Get IP address defined in domain's root A-record
  ipaddress=`dig $domain +short`

  # Get list of all nameservers
  ns=`dig ns $domain +short| sort | tr '\n' ','`

  # Use the line below to extract any information from whois
  # ns=`whois $domain | grep "Name Server" | cut -d ":" -f 2 |  sed 's/ //' | sed -e :a -e '$!N;s/ \n/,/;ta'`

  echo "$domain"

  # Uncomment the following lines to output the information directly into the terminal
  # echo "IP Address:  $ipaddress"
  # echo "Nameservers: $ns"
  # echo " "
  
  # Prints all the values fetched into the CSV file
  echo -e "$domain,$ipaddress,$ns" >> domains.csv

# Defines the text file from which to read domain names
done < domains.txt

3. Create the file where the results will be written:

# touch domains.csv

4. Execute the script:

# sh domains.sh

The domains.csv will have the list of the domains, A record, and nameservers:

Domain Name,IP Address,Nameserver,Nameserver,Nameserver,Nameserver,Nameserver
domain1.com,162.255.165.197,ns5.plothost.com.,ns6.plothost.com.
domain2.com,162.255.165.197,ns5.plothost.com.,ns6.plothost.com.
....

Leave a Reply