Last active
February 29, 2016 01:55
-
-
Save gb-swatanabe/f8ee3d5d154358162640 to your computer and use it in GitHub Desktop.
AWS Route53に登録したゾーン情報をAPIで引っ張ってきてBIND準拠の書式で出力(要awscli)
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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
require 'json' | |
opt = { | |
:aws => { | |
:profile => "<awscliのプロファイル名>", | |
:hosted_zone_id => "/hostedzone/<ゾーンID> | |
} | |
} | |
cmd = "aws --profile %s route53 list-resource-record-sets --hosted-zone-id \"%s\"" % [opt[:aws][:profile], | |
opt[:aws][:hosted_zone_id] | |
] | |
zones = JSON.parse(%x( #{cmd} )) | |
zones["ResourceRecordSets"].each{ |record| | |
tabs = [(5 - Integer(record["Name"].size / 8)),1].max | |
case | |
when record.has_key?("ResourceRecords") | |
# 通常のエントリ | |
record["ResourceRecords"].each{ |v| | |
puts "%s%s%7d IN\t%s\t%s" % [record["Name"], | |
"\t"*tabs, | |
record["TTL"], | |
record["Type"], | |
v["Value"] | |
] | |
} | |
when record.has_key?("AliasTarget") | |
# エイリアス | |
case record["Type"] | |
when "A" | |
puts "%s%s%7d IN\t%s\t%s\t; %s ALIAS" % [record["Name"], | |
"\t"*tabs, | |
"300", | |
"CNAME", | |
record["AliasTarget"]["DNSName"].gsub(/^dualstack./,''), | |
record["Type"] | |
] | |
else | |
puts "; %s IN %s ALIAS %s" % [record["Name"], | |
record["Type"], | |
record["AliasTarget"]["DNSName"] | |
] | |
end | |
else | |
puts "; unknown recode set: %s" % [record.to_s] | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment