0

I'm trying to construct a script that will run a command using the contents of two files, and append the results of that command to another file.

My two files:

  1. nameservers - contains the addresses of my DNS.
  2. hostnames - contains hostnames that needs to be resolved against the nameservers.

I need to pass the contents of nameservers and hostnames to this command:

dig @[content of nameservers file] -t a [content of hostnames]

Then each run it will extract the query time value and append it into a file in the following format:

[nameserver1] [query time value]

Like:

1.1.1.1 100
2.2.2.2 120
3.3.3.3 115
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
Jon McEye
  • 3
  • 2

1 Answers1

0

here is a way:

#!/bin/bash
hostnames=`cat hostnamesfile`
cat nameserversfile | while read n; do
  echo "$hostnames" | while read h; do
    echo -n "$n "
    dig @$n -t a $h | grep 'Query time' | grep -o '[0-9]\+'
  done
done > resultsfile
webb
  • 4,180
  • 1
  • 17
  • 26
  • This is rather horrible. The [useless `cat`](http://www.iki.fi/era/unix/award.html) should be easy to fix and `read -r` is also easy to change, but the rest of it should really be similarly refactored. Hint: useless use of `grep`; maybe read the hosts file into an array if you don't want to read it multiple times. – tripleee Jun 29 '16 at 10:49
  • reasonable point, but based on the OP, i wanted to make the answer as understandable as possible. – webb Jul 03 '16 at 14:47