0

I am working on migrating a customer's WHM / cPanel account to a new server, however he has many, many sites hosted on the server that I need to retrieve their nameservers.

I'd like to create a bash script that parses a file (with each domain on a separate line), performs a dig and whois, finds the nameserver and IP, and then outputs the domain and its nameserver to another file.

I'm not very good with bash, but I have found and edited this script - but it doesn't seem to want to work at all. If anyone has any insight, that would be great. Thanks!

#!/bin/bash
# dig $line +short >> ip address 
# whois $line >> Lists full details including the name servers 
# whois $line | grep "Name Server" | cut -d ":" -f 2 | sed 's/ //' | 
# sed -e :a -e '$!N;s/ \n/,/;ta'`  
while read inputfile 
do 
echo $domain  
ipaddress=`dig $domain +short` 
nameserver=`whois $domain | grep "Name Server" | cut -d ":" -f 2 |    
sed 's/ //' | sed -e :a -e '$!N;s/ \n/,/;ta'` 
echo -e "$domain,$ipaddress,$nameserver" >> outputfile
done
amy
  • 185
  • 1
  • 3
  • 13
  • You aren't giving that first `sed` command any input data to work on (though that looks like it was meant to be part of the comment actually). Likewise you aren't giving the `read` command in the `while` loop any data to read from (usually redirected from a file or similar on the `done` line via ` – Etan Reisner Apr 14 '15 at 23:22
  • Yes you are indeed correct, that first `sed` was intended to be a comment. Ugh, I need to work on my bash. – amy Apr 14 '15 at 23:26
  • Is `inputfile` there supposed to be the file that contains the domains? Because the arguments to `read` are the variables to assign the read data to not the file/etc. to read the data from. `read` reads from standard input by default (so `read line – Etan Reisner Apr 14 '15 at 23:29
  • Well that pretty much got me to where I needed. Much appreciated Etan! Only problem now is that some of these domains are `.br` and the whois doesn't return nameservers, ugh. Nothing you can help me with though hah. Again, thank you! EDIT: Woah what am I thinking. I forgot about `dig ns`. Man is it Friday yet? :P – amy Apr 14 '15 at 23:39
  • You should not use whois to get a working domain name nameservers. list Just use the DNS. – Patrick Mevzek Jan 02 '18 at 19:59

1 Answers1

1

This will output all possible A and NS records of domains in given file:

#!/bin/bash
while read domain 
do 

for a in `dig $domain a +short`
do
    ipaddress="$ipaddress$a,"
done

for ns in `dig $domain ns +short`
do
    nameserver="$nameserver$ns,"
done 
echo "$domain,$ipaddress,$nameserver"
done

usage

Suppose script is named as resolver and the file is input. Do

cat input | ./resolver

(Some domains have more than one ip addresses and name servers)

madz
  • 1,803
  • 18
  • 45