Skip to content

Instantly share code, notes, and snippets.

@einichi
Created December 25, 2019 07:50
Show Gist options
  • Save einichi/84f6332f7ddfef945850a3cd35f6be54 to your computer and use it in GitHub Desktop.
Save einichi/84f6332f7ddfef945850a3cd35f6be54 to your computer and use it in GitHub Desktop.
Get IP Address of Hostname - Google App Script
function getIP(host, recordType) {
// Specify record type as per described here: https://en.wikipedia.org/wiki/List_of_DNS_record_types
// For example, 'A', 'AAAA', 'NS', etc...
// So your Google Sheets formula would be =getIP("www.example.com", "A")
var url = "https://dns.google.com/resolve?name=" + host + "&type=" + recordType;
var json = UrlFetchApp.fetch(url);
var response = JSON.parse(json);
var answer = response.Answer;
var ip = "";
if (typeof answer != "undefined") {
for (i = 0; i < answer.length; i++) {
ip = ip + JSON.stringify(answer[i].data) + " ";
}
return ip;
}
else return "No record";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment