#!/bin/bash function get_domain_entry { IFS='.' read -r -a domain <<< "$1" NS="8.8.8.8" count=0 pendingdomain="" for i in `printf '%s\n' "${domain[@]}"|tac`; do count=$((count+1)) pendingdomain="${i}.${pendingdomain}" for j in $NS; do NEXTNS=`dig @${j} +timeout=1 +time=1 +tries=2 +noall +authority +answer ${pendingdomain} NS | awk '{if($4 == "NS") {print $5}}' | tr '\n' ' ' | sed -e '/^;;/d'` if [ -n "$NEXTNS" ];then NS=$NEXTNS break fi done if [ "$count" == "${#domain[@]}" ]; then for j in $NS; do rawresult=`dig @${j} +timeout=1 +time=1 +tries=2 +noall +authority +answer ${pendingdomain} A ${pendingdomain} AAAA | sed -e '/^;;/d'` CNAME=`echo "$rawresult" | awk '{if($4 == "CNAME") {print $5}}' | head -n 1` ENTRY=`echo "$rawresult" | awk '{if($4 == "A" || $4 == "AAAA") {print $5}}'` if [ -n "$ENTRY" ]; then echo "$ENTRY" break elif [ -n "$CNAME" ]; then ENTRY=`get_domain_entry "$CNAME"` echo "$ENTRY" break fi done fi done } result=`get_domain_entry $1` echo $result | tr '\n' ' '