Issue
I am looking for a way to retrieve the main domain (naked) from a random domain/subdomain. What I am looking for is not a sed or awk command (as the domain is random) but some string with dig, host or nslookup that can actually show the naked domain. Any suggestion?
Example:
from www.bbc.co.uk -> bbc.co.uk
from www.google.com -> google.com
from subdomain.google.co.uk -> google.co.uk
from subdomain.ofasubdomain.google.com.au -> google.com.au
Solution
I'm not an expert on domain names - Based on https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains, with minor exception, all domains with 2 letter suffix will have main domain of something.bb.cc, and all other suffix (usually 3 letters), the main domain will be something.ccc
Using bash
domain=...
md=
p2='^(.*\.)?([^.]+\.[a-z]+\.[a-z][a-z])$'
p3='^(.*\.)?([^.]+\.(com|org|net|int|edu|gov|mil))$'
px='^(.*\.)([a-z]+)$'
# 2 letter country codes
if [[ "$domain" =~ $p2 ]] ; then
md=${BASH_REMATCH[2]};
# 3 letters legacy domain
elif [[ "$domain" =~ $p3 ]] ; then
md=${BASH_REMATCH[2]};
# All Other
elif [[ "$domain" =~ $px ]] ; then
md=${BASH_REMATCH[2]};
fi ;
echo "$domain -> $md"
Could extend to handle few 4 letter domain
Answered By - dash-o Answer Checked By - Dawn Plyler (WPSolving Volunteer)