1

This is my code.

#!/bin/bash

for domain in google.com twitter.com
do
        ns_record=$(dig NS +short $domain)
        echo "$domain" "," "$ns_record" >> output.csv
done

It generates an output which is mentioned in below pic.

enter image description here

I want output in the below format.

enter image description here

What should be my code?

2 Answers 2

2

You can save the ns_record output as an array and then loop over the array outputting at a column position of your choosing. You will have to make a special case for the first record output at the end of $domain, but that is all simple to do with printf.

For example, you can modify your script to do:

#!/bin/bash

declare -i col2pos=20     ## set column 2 position

for domain in google.com twitter.com; do
  ns_record=($(dig NS +short $domain))    ## create array of dig output
  
  nsspaces=$(( col2pos - ${#domain} ))    ## get no. of chars in domain
  
  ## output domain followd by nsspaces spaces and first element of ns_record
  printf "%s%*s%s\n" "$domain" "$nsspaces" " " "${ns_record[0]}"
  
  ## loop outputting remaining ns_record at col2pos
  for ((i=1; i< ${#ns_record[@]}; i++)); do
    printf "%*s%s\n" "$col2pos" " " "${ns_record[i]}"
  done
  
done

(you can set $col2pos as you like)

Example Use/Output

$ bash ns_record.sh
google.com          ns2.google.com.
                    ns1.google.com.
                    ns3.google.com.
                    ns4.google.com.
twitter.com         a.r06.twtrdns.net.
                    d.r06.twtrdns.net.
                    ns4.p34.dynect.net.
                    ns1.p34.dynect.net.
                    d01-01.ns.twtrdns.net.
                    c.r06.twtrdns.net.
                    d01-02.ns.twtrdns.net.
                    b.r06.twtrdns.net.
                    ns2.p34.dynect.net.
                    ns3.p34.dynect.net.

Sorted ns_record

You may want the output of the ns_record information to appear sorted. You can easily do that piping the output of dig to sort, e.g.

  ns_record=($(dig NS +short $domain | sort)) ## create array and sort

Sorted Output

Now your output appears as:

$ bash ns_record.sh
google.com          ns1.google.com.
                    ns2.google.com.
                    ns3.google.com.
                    ns4.google.com.
twitter.com         a.r06.twtrdns.net.
                    b.r06.twtrdns.net.
                    c.r06.twtrdns.net.
                    d.r06.twtrdns.net.
                    d01-01.ns.twtrdns.net.
                    d01-02.ns.twtrdns.net.
                    ns1.p34.dynect.net.
                    ns2.p34.dynect.net.
                    ns3.p34.dynect.net.
                    ns4.p34.dynect.net.

I find that a bit easier to look through.

Let me know if you have questions.

Sign up to request clarification or add additional context in comments.

4 Comments

This is a Good solution. Just one question, the dig command is it a default *NIX utility? I am yet to check it but never used it honestly, cheers.
@RavinderSingh13 Yes, it is a Linux utility (DNS Information Groper), but there is no reason it can't be ported to run on the dark-side. I just suspect that windows net ... has a similar utility. (never had any reason to look over there, always had Nix at the fingertips :)
Excellent, well explained answer
Every once in a while I get one right :)
0

Using awk with formatted printf output:

#!/bin/bash

for domain in google.com twitter.com ; do
    col1="$domain"
    for col2 in $(dig NS +short "$domain") ; do
        printf '%-15s %s\n' "$col1" "$col2"
        col1=" "
    done
done 
google.com      ns1.google.com.
                ns3.google.com.
                ns4.google.com.
                ns2.google.com.
twitter.com     b.r06.twtrdns.net.
                d01-01.ns.twtrdns.net.
                d.r06.twtrdns.net.
                d01-02.ns.twtrdns.net.
                ns2.p34.dynect.net.
                ns1.p34.dynect.net.
                a.r06.twtrdns.net.
                c.r06.twtrdns.net.
                ns4.p34.dynect.net.
                ns3.p34.dynect.net.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.