Recent Additions‎ > ‎

NSLOOKUP with Google Apps Script

posted Mar 15, 2018, 2:53 AM by Waqar Ahmad   [ updated Dec 17, 2019, 5:48 PM ]
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 and 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.
nslookup Apps Script Tutorial

Google Public DNS Provides  API 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 = ''; // Google Pubic DNS API Url
  var type = 'MX'; // Type of record to fetch, A, AAAA, MX, CNAME, TXT, ANY
  var name = ''; // 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 ={return}).join('\n'); // Get the values
  Logger.log(answers); // Log the Values

After running the above code. Values will be logged in the Logger.

For more details on API, you can check the Google Documentation