NSLOOKUP with Google Apps Script
Post date: Mar 15, 2018 9:53:5 AM
Anyone who deals with public DNS must have used NSLOOKUP Command Line Tool available in major Operating Systems. Google provides public DNS servers with IP Addresses 8.8.8.8 and 8.8.4.4 which we usually use to resolve a domain name and find value for the various type of DNS records. Traditional DNS queries and responses are sent over UDP or TCP.
Google Public DNS Provides API https://dns.google.com/resolve? which can be used in Apps Script program with UrlFetchApp to fetch the results from Google Public DNS Server.
Here is a sample code.
var api_url = 'https://dns.google.com/resolve'; // Google Pubic DNS API Url
var type = 'MX'; // Type of record to fetch, A, AAAA, MX, CNAME, TXT, ANY
var name = 'sbctech.net'; // The domain name to lookup
var requestUrl = api_url + '?name=' + name + '&type=' + type; // Build request URL
var response = UrlFetchApp.fetch(requestUrl); // Fetch the reponse from API
var responseText = response.getContentText(); // Get the response text
var json = JSON.parse(responseText); // Parse the JSON text
var answers = json.Answer.map(function(ans){return ans.data}).join('\n'); // Get the values
Logger.log(answers); // Log the Values
After running the above code. Values will be logged in the Logger.
5 alt1.aspmx.l.google.com.
5 alt2.aspmx.l.google.com.
10 alt3.aspmx.l.google.com.
1 aspmx.l.google.com.
10 alt4.aspmx.l.google.com.
For more details on API, you can check the Google Documentation