Let’s say you have a text file containing a list of domains you want to query using whois. Our goal is to query each domain in the list an extract the nameservers for each domain.
This can be achieved using a bash script with a combination of whois, grep, and tr.
Examine the following code:
#!/bin/bash
input="domainlist.txt"
while IFS= read -r line
do
WHOISOUTPUT=$(whois $line | grep "Name Server" | tr -d '\n')
echo "$line," "$WHOISOUTPUT"
sleep 3
done < "$input"
This script instructs bash to run a whois command on each line of the input file. The variable $line will contain the domain name, grep will pick out the lines containing “Name Server”, and tr -d ‘\n’ will remove the new lines from the preceding text in the pipe so the output will appear on one line.
So your output will look something like:
domain.com, Name Server: ns1.nameserver.com Name Server: ns2.nameserver.com domain2.com, Name Server: NS1.MYDNS.com Name Server: ns2.MYDNS.com
We are Grepping “Name Server” because that follows the output format outlined by ICANN https://www.icann.org/resources/pages/approved-with-specs-2013-09-17-en#whois
Please be aware that sometimes whois servers can return non-standard formats.
The ‘Sleep 3’ statement is there just to be polite and not hammer nameservers.
If you pipe the output of this script to a text file and save it, you can open it as a CSV file.