Dns subnet

From Wiki
Jump to navigation Jump to search
#!/bin/bash
# find all known dns entries for a class C subnet
# subnet (e.g. 123.45.67)
subnet=123.45.67
host=/usr/bin/host
if [[ $host == "" ]]; then
    echo "host program not found"
    echo "aborting..."
    exit 1
fi
if [[ ! -x $host ]]; then
    echo "host program $host not executable"
    echo "aborting..."
    exit 1
fi

echo "Known DNS entries for subnet $subnet on `date`"

# main loop
for (( i=1; i <= 254; i++))
do
  ip=${subnet}.$i
  output=`$host $ip 2> /dev/null` 
  #echo "output is \"$output\""
  notfound=`echo $output | grep "not found"`
  if [[ -z $notfound ]]; then
      hostname=`echo $output | awk '{print $5}'`
      echo -e "${ip}\t${hostname}"
  fi
done

exit 0