Blog

cPanel: Find Duplicate Domains in Servers

Published on: April 24, 2019 by Smith Nevil

cPanel: Find Duplicate Domains in Servers

Scenario:

One of our client needed to enable DNS cluster for his web servers. Before enabling cluster we have to make sure there are no duplicate accounts in the server. In many cases, accounts were migrated across the servers and the previous ones were not deleted. So, when enabling the cluster, the DNS of the first synced server will be saved in Cluster, thus causing the website to point to the wrong server. Our requirement was to find all the duplicate accounts.

Pre-Requisites:

  • Create a directory named Servers and copy /etc/userdomains of all the servers are to be copied and saved to a file with name as its hostname. Eg: File server1.example.com contains the userdomains of the server server1.example.com.
  • All the IPs on the servers has rdns setup to with respective hostname.

If rdns is not set as the hostnames, the script will need to be modified to read IP and server mapping from another file.
Once the server list is added, the directory structure should look like the picture below: Here, dupdomain.sh is the name of the script.

 

 

Script Execution:

After all the servers are added, execute the script using /bin/sh dupdomain.sh. The script may take a few minutes to complete to execute, depending on the number of domains present. When the script has completed execution the output will be saved to a CSV file Duplicate_Domains.csv

CSV file contains the list of accounts which are multiple servers, along with the list of servers having the duplicate accounts. An example of the output file is shown below:

#!/bin/bash
#Date: 20190215

#Script to read list of domains in multiple cpanel server and find duplicates and show where the domain is actually pointed to.

ServerList=Servers #Copy the userdomains file to this path

echo "Domain,Duplicates,Original" > Output.csv
echo "" > Common.txt

for i in `ls -1 $ServerList | grep -v "'"`
do
awk '{print $1}' $ServerList/$i | rev |cut -c2-| rev > $ServerList/$i.tmp #Cut only the domain names from userdomains
done


for i in `ls -1 $ServerList | grep -v "'" | grep tmp` #loop the Serverlist
do
#echo $i

for j in `cat $ServerList/$i` #loop the domains
do
#echo $j

#Find domains in multiple servers

DUPWC=$(grep -w ^$j $ServerList/*.tmp | grep -v $i | cut -f1 -d ":"| wc -l) #check if found in multiple files
GRP=$(grep -c $j Common.txt)

if [ $GRP -gt 0 ]; #This is to remove duplicate check
then
:
else

while [ $DUPWC -gt 0 ]
do
echo "$j" >> Common.txt

IP=$(dig +short $j) #Original IP

RDNS=$(dig -x $IP +short | head -1 | rev| cut -c2- | rev) #Hostname

DUP=$(grep -w ^$j $ServerList/*.tmp | cut -f1 -d ":"| cut -c9- | rev | cut -c5- | rev | grep -v $RDNS )

echo $j, $DUP, $RDNS >> Output.csv

break

done
fi
done
done

rm -f $ServerList/*.tmp
#rm -f Common.txt

Category : cPanel, Sever management

Smith Nevil

Smith Nevil

Smith is always ready to learn new technologies and explore new territories. His never-ending passion towards technological advancements, unyielding affinity to perfection and excitement in the exploration of new areas, help him to be on the top of everything he is involved with. He is currently working as System Engineer at SupportSages.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend