Jack
Jack

Reputation: 301

Checking for domain availability in bash with dig

I am currently trying to write a script that will write out domains that it detects are available. The first idea was to write out any that include "NXDOMAIN", but this ended up including domains that I found I couldn't register.. so I added a requirement for "a.gtld-servers.net" also. The problem is, both of these conditions are being met on domains are ARE registered already. I'm fresh out of ideas with regards to what I can use to filter my results.

Does anyone have any idea? here is my code:

function getResponse () {
   output=$(dig $1.com +nostats +noanswer +noquestion)
     if [[ $output == *NXDOMAIN* ]] && [[ $output == *a.gtld-servers.net.* ]];  then
   echo "$1.com"
     fi
 }

for v in {a..z}; do
  for w in {a..z}; do
    for x in {a..z}; do
      getResponse $v$w$x &
      sleep 0.01s
    done
  done
done

for v in {a..z}; do
  for w in {a..z}; do
    for x in {a..z}; do
      for y in {a..z}; do
        getResponse $v$w$x$y &
        sleep 0.01s
      done
    done
  done
done

Upvotes: 2

Views: 3653

Answers (1)

Edouard Thiel
Edouard Thiel

Reputation: 6208

Here is a script which seems to work:

#! /bin/bash

do_query () # name
{
    dig "$1" +noquestion +nostat +noanswer +noauthority 2> /dev/null
}

get_answers_number () # name
{
    local res=$(do_query "$1")
    res=${res##*ANSWER: }
    echo "${res%%,*}"
}

# Unregistered domains saved in file
file="unregistered.txt"
echo "Results will be saved in $file"

for adr in {a..z}{a..z}{a..z} {a..z}{a..z}{a..z}{a..z}
do
    name="$adr.com"
    printf "Checking %s ...\r" "$name"
    r=$(get_answers_number "$name")
    if ((r==0)); then
        echo "Found $name            "
        echo "$name" >&3
    fi
done 3>| "$file"

Upvotes: 5

Related Questions