Search code examples
bashshelldnssubdomain

how to distinguish the domain from a subdomain


i have a problem with script. At my work i often have to check PTR of domain, MX records and similar data. I did the most of the script, but the problem appears when i do something like
this is subdomain

e-learning.go4progress.pl

this is domain but second level?

simple.com.pl

Now in my script there is something like: if i don't put www, it adds and does

dig www.e-learning.go4progress.pl
dig e-learning.go4progress.pl
dig go4progress.pl

he counts dots and subtracts 1, but problem is when domain looks like

simple.com.pl

because script make dig too for

com.pl

I don't check many domain which contains com.pl, co.uk, gov.pl. I've got an idea to make array and compare this what i put in script with array, when it finds in string component of array he subtracts 2 instead 1 ;) I paste a part of script to better understand me why he substracks 1.

    url="`echo $1 | sed 's~http[s]*://~~g' | sed 's./$..' | awk '!/^www/ {$0="www." $0}1'`"
ii=1
dots="`echo $url | grep -o "\." | wc -l`"
while [ $ii -le $dots] ; do
        cut="`echo $url | cut -d "." -f$ii-9`"
        ip="`dig +short $cut`"
        host="`dig -x $ip +short`"
        if [[ -z "$host" ]]; then
                host="No PTR"
        fi
        echo "strona: $cut  Host: $host" 
        ii=$[ii + 1]

Maybe you have diffrent idea how to help with my problem.

Second question is how to distinguish subdomain from domain.
I need compare mx records of subdomain(when the url contains subdomain) and mx records top level domain.
I waiting for your response ;)


Solution

  • My solution to find the domain as registered with the registrar:

    wget https://raw.githubusercontent.com/gavingmiller/second-level-domains/master/SLDs.csv
    
    DOMAIN="www.e-learning.go4progress.co.uk";
    KEEPPARTS=2;
    TWOLEVELS=$( /bin/echo "${DOMAIN}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter=".\\" -f 1-2 | /usr/bin/rev );
    if  /bin/grep -P ",\.${TWOLEVELS}" SLDs.csv >/dev/null;  then
        KEEPPARTS=3;
    fi
    DOMAIN=$( /bin/echo "${DOMAIN}" | /usr/bin/rev | /usr/bin/cut -d "." -f "1-${KEEPPARTS}" | /usr/bin/rev );
    echo "${DOMAIN}"
    

    Thanks to https://github.com/gavingmiller/second-level-domains and https://github.com/medialize/URI.js/issues/17#issuecomment-3976617