FLASH SALE Get 20% OFF everything using the coupon code: FLASH20 View WordPress Plugins →

Check DNS records in bulk using JavaScript

on in JavaScript Arrays & Objects, AJAX and Fetching Data
Last modified on

If you’re a web developer or network administrator, you know that DNS records play a crucial role in ensuring that websites and other network services are accessible. DNS (Domain Name System) is a protocol that translates domain names into IP addresses and other information needed for communication over the internet. There are several types of DNS records, each with its own purpose.

The JavaScript I’ll be showing you today is a handy tool for checking specific DNS records using a <textarea> element for domains, where you can bulk paste URLs (one per row). The code makes use of the Google Public DNS service to query MX records for a list of domains and display the TXT records associated with them. I wrote this code to allow a third party to check for various domain configuration, such as the presence of Google Suite in MX records, or the presence of DKIM keys and their individual values. The script below is a starting point, as there is no configuration, everything is done inside the code, such as changing the record type, ID, or display value.

Before diving into the code, let’s take a look at the different types of DNS records and their corresponding type IDs:

Record TypeType ID
A (Address)1
AAAA (IPv6 Address)28
CNAME (Canonical Name)5
MX (Mail Exchange)15
NS (Name Server)2
PTR (Pointer)12
SOA (Start of Authority)6
SRV (Service)33
TXT (Text)16

As you can see, each record type has a unique ID that can be used to query that specific type of record.

document.getElementById("go").addEventListener("click", () => {
    const textarea = document.getElementById("domains");
    const domains = textarea.value.split("\n");
    domains.forEach((line) => {
        const words = line.trim().split(" ");
        const domain = words[0];
        const url = `https://dns.google/resolve?name=${domain}&type=MX`; // google._domainkey
        fetch(url)
            .then((response) => response.json())
            .then((data) => {
                const txtRecords = data.Answer.filter(
                    (record) => record.type === 15
                );
                console.log(`MX records for ${domain}:`, txtRecords);
                document.getElementById(
                    "results"
                ).innerHTML += `<br>MX records for ${domain}:<br>`;

                txtRecords.forEach((record) => {
                    console.log(record.data);
                    document.getElementById("results").innerHTML += `<div><code>${record.data}</code></div>`;
                });
            })
            .catch((error) => {
                console.error(
                    `Error checking MX records for ${domain}:`,
                    error
                );
            });
    });
});

The script loops through the array of domains and, for each domain, it extracts the domain name from the line and creates a URL that queries the Google Public DNS service – ⁣https://dns.google/resolve⁣ – for the MX record of the domain. The URL is constructed using a template literal that allows me to insert the domain name and the type ID of the record I want to query dynamically.

The script then uses the Fetch API to send an HTTP GET request to the URL I just constructed. The Fetch API returns a Promise that resolves to the response from the server. I chained two then() methods to the Promise: the first then() method extracts the JSON content from the response using the json() method and returns it as a Promise. The second then() method processes the JSON content and extracts the value from the MX record using the filter() method. The filter() method takes a function that tests each record in the Answer array and returns true if the record type is 15 (which corresponds to the MX record).

The code then logs the values to the console and adds them to a results section in the HTML. That’s all. It’s not complicated at all, and it serves as a great starting point for a small app, where users can select their own record types and also validate, sanitize and clean up a list of URLs.

You can see a demo here.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *