Multiple DNS lookups with Bash

 
 

Multiple DNS lookups with Bash scripting for a predefined list of domain names, more fun but this time using our bash scripting skills. We all know and must agree that Bash is still a very powerful tool when properly used, we can script pretty much anything at the OS level from small tasks like removing files, truncating logs or initiating backups up to more large scale tasks like deploying code locally or even on remote servers. In this really small tutorial we will guide you how to perform multiple DNS lookups by simply scripting this task in Bash.

Table of contents

Scenario
Create .sh file
DNS Lookup script
Run the script
DNS Lookup results

Scenario

In this particular tutorial we will assume that we have a list of websites / domain names that we need to check if they are pointing to the right servers, basically if their public IPs are the correct ones. For this we have to create first a filename called list_of_websites.txt where to enumerate all domain names that we are looking for. Listed below we have the listed the content of list_of_websites.txt file:


google.com
amazon.com
ebay.com

Create .sh file

All good so far, we have the domains list and now let’s create a .sh file for our bash script in order to perform DNS lookups. First we need to create our file using touch command, we will make this file executable with chmod command followed by +x argument and finally we will open it for editing using vi editor, so lets run the next commands within our terminal window as shown below:


$ touch dns_lookup.sh
$ chmod +x dns_lookup.sh
$ vi dns_lookup.sh

DNS Lookup script

On this particular step all the magic happens, basically this is the step where we have to write the script that pulls the data from our domains list file, it performs all necessary DNS lookups and saves the results to a filename called results.txt. For visual feedback we have added some messages to appear on our console during the task like Start, Checking now:, Next task: and finally Done! when all DNS lookups are completed. So let’s add the lines below within our dns_lookup.sh file. Now you may save and close the file as we are moving to the next step where we will test this Bash script.


#!/bin/sh

filename="list_of_websites.txt"

echo Start

while read p; do
    echo "Checking now:" $p
    echo "Website:" $p >> results.txt
    nslookup $p | grep "Address" >> results.txt
    echo "Next task..."
    echo "---------------------------------------" >> results.txt
done < $filename

echo Done!

Please note that we have used #!/bin/sh at the beginning of the file and not #!/bin/bash this is simply because in this tutorial we have said that we are using Bash, in this case /bin/sh being symlinked to /bin/bash but please feel free to replace it if you really want to.

Run the script

Knowing that our file it is executable, we have the script in place to perform DNS Lookup against our domains list file we can simply run the file and see what happens next, so let's simply execute the file like in the example below:


$ ./dns_lookup.sh

DNS Lookup results

A successful console output should look similar to this one listed below where we can clearly see that our script had no issue in terms of execution and all echo messages were correctly displayed on our terminal window.


Start
Checking now: google.com
Next task...
Checking now: amazon.com
Next task...
Checking now: ebay.com
Next task...
Done!

Apart from those messages shown on our terminal window we have no other messages that shows us if the DNS Lookup process went fine. So let have a look over our results.txt file where we should be able to see actually the expected result, we will check that by using cat command as shown below:


$ cat results.txt

The output should looks like this one below even though we have intentionally removed the real IP addresses.


Website: google.com
Address: [IP Address]
---------------------------------------
Website: amazon.com
Address: [IP Address]
---------------------------------------
Website: ebay.com
Address: [IP Address]
---------------------------------------

If everything went fine then we can say that our short tutorial called Multiple DNS lookups with Bash ends here.

Video

No video posted for this page.

Screenshots

No screenshots posted for this page.

Source code

No code posted for this page.

About this page

Article
Multiple DNS lookups with Bash
Author
Category
Published
07/02/2018
Updated
27/11/2018
Tags

Share this page

If you found this page useful please share it with your friends or colleagues.