Activities

October 2014
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Bash : Find the MX records details of a domain name from a file list

Prior to setup a Namse server setup using powerDNS. I want to know whether the domain names are using any external smtp servers or Not. It’s because, If they are using external smtp, I need to contact those customer as well as create the DNS, MX records ass well.

Source

I have given a list of domains names and current Name servers used for the server.

Requirement

I need to know any of the domains are using emails other than my server. If so, I need to find the MX record of the those domain and set DNS record.

Listing the MX record of the domains those are using External EMAIL service.

Download the script file here

#!/bin/bash
domain_list_file="/root/OR_Domain_names.txt"
#domain_list_file="/root/test1.txt"
ext_domain_file="/root/exterenal_mxdomains.txt"
touch $ext_domain_file
nameserver_check="NS1.MYDOMAIN.COM"
cat /dev/null > $ext_domain_file
counter=0
ext_count=0
while read line
do

name_server_name="$(whois $line | grep "Name Server" | awk '{print $3}'|head  -n 1)"
server_name="$(host -t mx  $line  |  awk '{print $7}' |head  -n 1)"
mail_server_name="$(echo $server_name | rev | cut -c 2- | rev)"
#clear
echo "Domains:: $counter, Executing:: $line , NS : $name_server_name   Mail Host : $mail_server_name , MX-Domains :: $ext_count "
#checking if blank line is present
 if [ "$mail_server_name"  == ""   ]
 then
 echo ""
 else
    if [ "$name_server_name"  == "$nameserver_check"   ]
    then
        if [ "$mail_server_name"  == "$line"   ]
        then
        echo ""
        elif [ "$mail_server_name"  == ""   ]
        then
        #echo "$line  does not have any MX record set"  >> $ext_domain_file
        echo ""
        else
                echo " $line using external MX"
        mxhost="$(host -t mx  $line | awk '{print $7}' |head  -n 1)"
        ext_count=$((ext_count+1))
        echo  "#########  $line   ######## "    >>  $ext_domain_file
        echo "             MX Host                     Priority "  >>  $ext_domain_file
        host -t mx  $line  | while IFS= read -r line1 ; do
        echo  $line1 | grep " " | awk '{print "          " $7"        "  ,$6 }'  >> $ext_domain_file
        done
        echo .
        echo .

        fi
    else
        echo ""
    fi


fi


counter=$((counter+1))
 
done <"$domain_list_file"

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>