このチュートリアルでは、DNS over HTTPS(DoH)を使って Cloudflare の 1.1.1.1 DNS リゾルバーに照会する、Google スプレッドシートのカスタム関数を作成します。DoH は、HTTPS 上で DNS 照会を暗号化するプロトコルです。設定が終わると、任意のセルに =NSLookup("A", "example.com") のような数式を入力して、スプレッドシートから離れずに DNS レコードを取得できます。多数のドメインをまとめて監査する場合、移行を計画する場合、DNS の変更を監視する場合に便利です。
まず、Google スプレッドシートを開き、次のコードで Google Apps Script のカスタム関数 ↗ を作成します。
function NSLookup(type, domain, useCache = false, minCacheTTL = 30) {
// --- Parameter validation ---
if (typeof type == "undefined") {
throw new Error("Missing parameter 1 dns type");
}
if (typeof domain == "undefined") {
throw new Error("Missing parameter 2 domain name");
}
if (typeof useCache != "boolean") {
throw new Error("Only boolean values allowed in 3 use cache");
}
if (typeof minCacheTTL != "number") {
throw new Error("Only numeric values allowed in 4 min cache ttl");
}
type = type.toUpperCase();
domain = domain.toLowerCase();
// --- Optional caching layer (uses Google Apps Script CacheService) ---
let cache = null;
if (useCache) {
// Cache key and hash
cacheKey = domain + "@" + type;
cacheHash = Utilities.base64Encode(cacheKey);
cacheBinKey = "nslookup-result-" + cacheHash;
cache = CacheService.getScriptCache();
const cachedResult = cache.get(cacheBinKey);
if (cachedResult != null) {
return cachedResult;
}
}
// --- DNS-over-HTTPS query to Cloudflare's 1.1.1.1 resolver ---
const url =
"https://cloudflare-dns.com/dns-query?name=" +
encodeURIComponent(domain) +
"&type=" +
encodeURIComponent(type);
const options = {
muteHttpExceptions: true,
headers: {
accept: "application/dns-json",
},
};
const result = UrlFetchApp.fetch(url, options);
const rc = result.getResponseCode();
const resultText = result.getContentText();
if (rc !== 200) {
throw new Error(rc);
}
// --- Standard DNS response codes ---
const errors = [
{ name: "NoError", description: "No Error" }, // 0
{ name: "FormErr", description: "Format Error" }, // 1
{ name: "ServFail", description: "Server Failure" }, // 2
{ name: "NXDomain", description: "Non-Existent Domain" }, // 3
{ name: "NotImp", description: "Not Implemented" }, // 4
{ name: "Refused", description: "Query Refused" }, // 5
{ name: "YXDomain", description: "Name Exists when it should not" }, // 6
{ name: "YXRRSet", description: "RR Set Exists when it should not" }, // 7
{ name: "NXRRSet", description: "RR Set that should exist does not" }, // 8
{ name: "NotAuth", description: "Not Authorized" }, // 9
];
const response = JSON.parse(resultText);
if (response.Status !== 0) {
return errors[response.Status].name;
}
// --- Extract answer records and determine cache TTL ---
const outputData = [];
let cacheTTL = 0;
for (const i in response.Answer) {
outputData.push(response.Answer[i].data);
const ttl = response.Answer[i].TTL;
cacheTTL = Math.min(cacheTTL || ttl, ttl);
}
const outputString = outputData.join(",");
if (useCache) {
cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL));
}
return outputString;
}NSLookup 関数をレコードタイプとドメインで呼び出すと、セルに対応する DNS レコードの値が表示されます。ドメインとレコードタイプに対して DNS が返すデータ(IP アドレスなど)です。
関数のシグネチャは次のとおりです。
=NSLookup(type, domain, useCache, minCacheTTL)
| パラメーター | 必須 | デフォルト | 説明 |
|---|---|---|---|
type |
はい | — | 照会する DNS レコードタイプ(例: A、AAAA、MX)。 |
domain |
はい | — | 照会するドメイン名。 |
useCache |
いいえ | false |
true にすると、Google Apps Script の CacheService で結果をキャッシュします。大きなスプレッドシートでの繰り返し DNS 照会を減らせます。 |
minCacheTTL |
いいえ | 30 |
キャッシュの最短保持時間(秒)。実際の TTL は、この値と DNS レスポンスの TTL のうち大きい方です。 |
対応している DNS レコードタイプ
AAAAACAACNAMEDSDNSKEYMXNSNSECNSEC3RRSIGSOATXT
たとえば、セル B1 に A(レコードタイプ)、B2 に example.com(ドメイン)が入っているとき、別のセルに次の数式を入力します。
=NSLookup(B1, B2)地域の設定によっては、引数の区切りにセミコロンが必要な場合があります。
=NSLookup(B1; B2)
そのドメインの A レコードが返されます。
198.41.214.162, 198.41.215.162