Search code examples
regexbashdig

How can I control de output in bash using dig?


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


Solution

  • 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}'