0

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 8.8.8.8 +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.

#!/bin/bash
#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
    # Do a reverse lookup
    dig -x $line +short
done

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

Assuming reverse_dns_lookup.in contains

-x 8.8.8.8
-x 127.0.0.1

Then:

$ dig -f reverse_dns_lookup.in +short
google-public-dns-a.google.com.
localhost.

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

8.8.8.8.in-addr.arpa. google-public-dns-a.google.com.
1.0.0.127.in-addr.arpa. localhost.

in dig-results.txt.

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

while read line
do
        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. – frankguthrie Oct 11 '17 at 10:09
  • @frankguthrie: It is unclear what you are intending to accomplish. Please make your question more understandable. – DevilaN Oct 11 '17 at 10:11
  • Input file for the reverse lookup is reverse_dns_lookup.in At the moment there is 1 line with 8.8.8.8 (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. – frankguthrie Oct 11 '17 at 10:14
  • Answer updated accordingly – DevilaN Oct 11 '17 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 – frankguthrie Oct 11 '17 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.