I'm trying to make a shell script to do a reverse DNS lookup but I can't get it to work.

Basically I want to do this:

dig -x +short 

The output will be this: google-public-dns-a.google.com. I want to send that to an output file.

At the moment my code looks like this.

#Read a file where per line there will be an IP address. The .in file is the input/sourcelist from which IP addresses are read
cat reverse_dns_lookup.in | while read line
    # Do a reverse lookup
    dig -x $line +short

When I run the script it seems nothing is happening, so my thought was that I would not call dig but first test the script like this:

echo dig -x $line +short

Even this does not produce any output. What am I missing here?

2 Answers 2


Assuming reverse_dns_lookup.in contains



$ dig -f reverse_dns_lookup.in +short

To add the -x to the contents of your existing file and call dig without modifying the file, using process substitution:

$ dig -f <( sed 's/^/-x /' reverse_dns_lookup.in ) +short

This avoids calling dig multiple times in a loop, and it avoids parsing the reverse_dns_lookup.in file with read.

Then redirect the output to a file of your choice:

$ dig -f <( sed 's/^/-x /' reverse_dns_lookup.in ) +short >dig-results.txt

To get be able to pair up the IP address with the result for the successful queries:

$ dig -f <( sed 's/^/-x /' reverse_dns_lookup.in ) +noall +answer | awk '{ print $1, $NF }' >dig-results.txt

For the example file I used, this will give google-public-dns-a.google.com. localhost.

in dig-results.txt.

  • Oke thanks, but i;m still missing the IP address from the output. I want the output like this - google-public-dns-a.google.com. without the ip in the output it's just the names. Commented Oct 11, 2017 at 11:22
  • @frankguthrie See updated answer.
    – Kusalananda
    Commented Oct 11, 2017 at 11:26

while read line
        echo $line - `dig -x "$line" +short`
done < reverse_dns_lookup.in

This code is working fine for me. You have to make sure, that file reverse_dns_lookup.in is in right place thou.

To send output from script to file just redirect it using standard stdin redirection operator in bash: ./script.sh > output_file.txt

Also answering Your concerns from change has been made to show IP alongside with revdns entry.

  • Does work for me... so the reverse_dns_lookup.in is the source file, and i want either send it t o another log file or to the terminal. Are you sure the filename is correct where you placed it? The reverse_dns_lookup.in is in the same directory as the script. Commented Oct 11, 2017 at 10:09
  • @frankguthrie: It is unclear what you are intending to accomplish. Please make your question more understandable.
    – DevilaN
    Commented Oct 11, 2017 at 10:11
  • Input file for the reverse lookup is reverse_dns_lookup.in At the moment there is 1 line with (google). I just want a reverse lookup on a source file with multiple ip addresses, 1 IP address per line. The output can be sent either to the terminal or to an output/log file. a rather simple action i would think. Commented Oct 11, 2017 at 10:14
  • Answer updated accordingly
    – DevilaN
    Commented Oct 11, 2017 at 10:26
  • I did get it to work now, not sure why it was not working before, seems my VM's lagging...i want to script the output. Now i'm using it as you showed, but with that i can;t manipulate the output. in the output file I want the ip address + the result Commented Oct 11, 2017 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .