Created
December 25, 2019 07:50
-
-
Save einichi/84f6332f7ddfef945850a3cd35f6be54 to your computer and use it in GitHub Desktop.
Get IP Address of Hostname - Google App Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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