如何区分域和子域

时间:2014-02-06 22:08:09

标签: bash shell dns subdomain

我的脚本有问题。 在我的工作中,我经常需要检查域名,MX记录和类似数据的PTR。我完成了大部分脚本,但是当我执行类似于这是子域

的操作时会出现问题
e-learning.go4progress.pl

这是域名但是第二级?

simple.com.pl

现在在我的脚本中有类似的东西:如果我不放www,它会添加并执行

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

他计算点数并减去1,但问题是当域看起来像

simple.com.pl

因为脚本也需要挖掘

com.pl

我不检查包含com.pl,co.uk,gov.pl的许多域名。我有一个想法制作数组并将其与我在脚本中放入数组的比较,当它在数组的字符串组件中找到时,他减去2而不是1;) 我粘贴了一部分脚本,以便更好地理解我为什么要进行替换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]

也许你有不同的想法如何帮助解决我的问题。

第二个问题是如何区分子域和域 我需要比较子域的mx记录(当url包含子域时)和mx记录顶级域 我在等你的回复;)

1 个答案:

答案 0 :(得分:0)

我找到在注册商处注册的域名的解决方案:

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}"

感谢https://github.com/gavingmiller/second-level-domainshttps://github.com/medialize/URI.js/issues/17#issuecomment-3976617