google-apps-scriptgoogle-sheetsnslookupdig

Nslookup or dig in Google App Script


Is there any way to run an nslookup / dig in Google App Script? I m looking to get the results of this command in a Google Spreadsheet.

Thanks


Solution

  • Find a web service that will let you use a GET or POST anonymously to query DNS info using a RESTful API, and you'll be able to use UrlFetchApp.fetch() to access it.

    For example, StatDNS has a simple API. Here's a custom function that will resolve a Domain Name to IPv4 Address.

    screenshot

    code

    /**
     * Peform a Network Service Lookup, using StatDNS API.
     *
     * @param {"google.com"} dn    A well-formed domain name to resolve.
     * @return {String}            Resolved IP address
     * @customfunction
     */
    function NSLookup(dn) {
      var url = "http://api.statdns.com/%FQDN%/a".replace("%FQDN%",dn);
      var result = UrlFetchApp.fetch(url,{muteHttpExceptions:true});
      var rc = result.getResponseCode();
      var response = JSON.parse(result.getContentText());
      if (rc !== 200) {
        throw new Error( response.message );
      }
      var ip = response.answer[0].rdata;
      return ip;
    }