#!/bin/bash

# 
# NSHierarch, extract a tree from a nameserver (and its defined sub name
# servers) containing hosts
#
# 26/04/2003
# Ferdinand Hagethorn <scripts -at- precompiled -dot- org
#


if [ b$1 = b ];then
  echo "This script will print out the nameserver + hosts layouted by subnet of a network"
  echo "Usage: $0 <domain.ext>"
  echo " e.g.: $0 microsoft.com  or enter a subdomain like:"
  echo "     : $0 ashholios.microsoft.com"
  echo 
  echo "Written by Ferdinand Hagethorn <scripts@precompiled.org>"  
  exit 1
fi


LOOKUPZ=$1

nservers() {
  for i in `host -t NS -l $1 2>/dev/null|awk {'print $1'}|sort -u`;do
    if [ b$i != b$1 ];then
      echo "$i"
    fi
  done
}

hosts() {
  for i in `host -l -t A $1 2>/dev/null|awk {'print $1'}| sort -u`;do
    echo "$i"
  done
}

##the functions can be called with:
#hosts <subdomain>
#nservers <subdomain>


lookemup() {
for i in `nservers $1`;do 
    echo "$2 +-  Nameserver: [$i]"
  for j in `hosts $i`;do
    echo "$2 |                \- $j"
  done

  space="$2 |               "
  lookemup $i "$space"
done
}

echo "Investigating domain: $LOOKUPZ"
echo "================================================================================"
echo "+ Nameserver: [$LOOKUPZ] "
for i in `hosts $LOOKUPZ`;do
  echo "|              \- $i"
done


lookemup $LOOKUPZ
echo "================================================================================"
