WordPress Developer & Linux Administrator

Bash script to DNS lookup for cPanel reseller

This bash script will take a given cPanel reseller, find all of its users, fetch all cPanel domains related to the user, and perform a dig DNS lookup on each of the cPanel domain names. By default, it performs a lookup of just an A record, however, you can customize the -t flag to any number of other DNS lookups. Such as: A, TXT, MX, NS, SOA, HINFO, or AXFR.

Example usage

Basic DNS lookup on each domain

./thisscript <reseller>
./thisscript <reseller> A

DNS lookup for other record types

./thisscript <reseller> TXT
./thisscript <reseller> MX
./thisscript <reseller> NS
./thisscript <reseller> SOA
./thisscript <reseller> HINFO
./thisscript <reseller> AXFR

Without further delay, here’s the script. Enjoy!

#!/bin/bash
#
#  Date: Dec 31st 2014
#  Author: Will Ashworth (williamashworth.com || linuxscripts.org)
#
#  Bash script to lookup DNS for cPanel reseller's accounts
#
#  Copyright (C) 2015 Will Ashworth
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details. http://www.gnu.org/licenses/

reseller=$1
lookup=$2

if [ -z "$reseller" ]
then
    echo "Reseller is empty. Please provide one! Like this..."
    echo "./thisscript <reseller>"
else
    if [ -z "$lookup" ]
    then
        lookup="A"
    fi

    # Get the users for this reseller
    users=`grep $reseller /etc/trueuserowners | cut -d : -f 1`

    # Loop through the list of users
    for user in $users; do
        # Get domains for the user
        domains=`grep ": $user" /etc/userdomains | cut -d: -f1`
        # Loop through the list of users
        for domain in $domains; do
            dig -t $lookup $domain +noall +answer
        done
    done
fi
Categories: Bash, cPanel, Scripting

Comments

Your email address will not be published. Required fields are marked *