2

I using the following bash to get DNS records of multiples domains inside domains.txt

#!/bin/bash
for domain in $(cat domains.txt); do dig @8.8.8.8 $domain ANY +multiline +noall +answer +nocmd; done

Current dig output is:

; <<>> DiG 9.8.3-P1 <<>> @8.8.8.8 aeapi.bluetail.salesforce.com ANY +multiline +noall +answer +nocmd
; (1 server found)
;; global options: +cmd
site.com. 59 IN A 0.0.0.1
site.com. 59 IN A 0.0.0.0

And I like get the output on the following format:

site.com,0.0.0.1
site2.com,0.0.0.2
site3.com,0.0.0.4

how can I do this? help would be appreciated

pancho
  • 176
  • 1
  • 2
  • 13

2 Answers2

1

To display only the IN A lines in comma-separated format:

for domain in $(cat domains.txt); do dig @8.8.8.8 $domain ANY +multiline +noall +answer +nocmd; done | awk '/IN A ([[:digit:]]+\.){3}/{gsub(/\.$/,"",$1); print $1","$NF}'

Or, for those who like their commands spread over multiple lines:

for domain in $(cat domains.txt)
do
   dig @8.8.8.8 $domain ANY +multiline +noall +answer +nocmd
done | awk '/IN A ([[:digit:]]+\.){3}/{gsub(/\.$/,"",$1); print $1","$NF}'

How it works

  1. /IN A ([[:digit:]]+\.){3}/{...}

    This selects lines that look like IN A followed by an IP address. (Actually, we only check for three sets of digits followed by a period but, here, that is good enough.) For those and only those lines, the commands inside curly braces are executed.

  2. gsub(/\.$/,"",$1)

    For the selected lines, we remove the period from the end of the first field.

  3. print $1","$NF

    For the selected lines, we print the first field, a comma, and the last field,

Aside

The command for domain in $(cat domains.txt) has some issues. In particular, the contents of domains.txt will be subjected to word-splitting and pathname expansion and that may cause unexpected behavior. Depending on the format of domains.txt, there will be much better ways of reading the contents of this file.

Adding double-quotes to the output

for domain in $(cat domains.txt); do dig @8.8.8.8 $domain ANY +multiline +noall +answer +nocmd; done | awk '/IN A ([[:digit:]]+\.){3}/{gsub(/\.$/,"",$1); printf "\"%s\",\"%s\"\n",$1,$NF}'
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks John1024. Now the output is: "domain.com. 21599 IN A 127.0.0.1" and I like to change the output to: "domain.com,127.0.0.1" (separated with a comma) – pancho Dec 22 '17 at 23:46
  • @pancho OK. See update for a way to just print the IP lines with comma-separated format. – John1024 Dec 22 '17 at 23:55
  • Hello again. How can I add " to the output for example: "3d.ebay.com","66.211.188.45" with print "$1"",""$NF" ?? – pancho Dec 23 '17 at 01:25
  • @pancho OK. I added to the end of the answer an example that puts double-quotes around the output. – John1024 Dec 23 '17 at 01:29
1

You can pipe the dig output into any other program. You may want to use sed, I'm more used to grep. grep -v -e "<regex>" will delete any line which matches the regular expression <regex>.

Let's explain the regular expression "^$\|^;" : ^$ stands for empty line, ^; stands for a line that start with ;, | means the logical operator OR, and \ escapes the | character (it tells grep that this is an operator and not a regular character).

So the command you may want would be like this :

$ dig stackoverflow.com | grep -v -e "^$\|^;"

stackoverflow.com.      1003    IN      A       151.101.1.69
stackoverflow.com.      1003    IN      A       151.101.193.69
stackoverflow.com.      1003    IN      A       151.101.129.69

Edit : if you want to get rid of the 1003 IN A you can pipe into the sed command :

sed "s/.\s*[0-9]*\s*IN\s*A\s*/, /".

The format is s (which stands for substitute) / matching regexp / what you want /

hugoShaka
  • 4,977
  • 3
  • 17
  • 29