8

将AWS Router53 DNS 导出文件转换成兼容 BIND的区域文件并导入到Azure DNS

 2 years ago
source link: https://blog.51cto.com/mooing/5569820
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

将AWS Router53 DNS 导出文件转换成兼容 BIND的区域文件并导入到Azure DNS

原创

在某些场景中,需要将AWS上的DNS迁移到其他服务商,如Azure,Dodaddy,Cloudflare等,AWS导出的DNS区域文件为Json格式,需要将其将换成兼容 BIND 的区域文件格式(RFC1035)。

AWS DNS区域文件格式:

{
"ResourceRecordSets": [
{
"Name": "mooing.cc.",
"Type": "NS",
"TTL": 3600,
"ResourceRecords": [
{
"Value": "ns-590.awsdns-09.net."
},
{
"Value": "ns-1393.awsdns-46.org."
},
{
"Value": "ns-339.awsdns-42.com."
},
{
"Value": "ns-1767.awsdns-28.co.uk."
}
]
},
{
"Name": "test.mooing.cc.",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "1.1.1.1"
}
]
},
{
"Name": "test2.mooing.cc.",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [
{
"Value": "www.baidu.com."
}
]
}
}

兼容 BIND 的区域文件格式(RFC1035):

test.mooing.cc. 300 IN A 1.1.1.1
test2.mooing.cc. 300 IN CNAME www.baidu.com.

编写了一个脚本,供大家参考:

需要注意某些记录(如例子中的NS记录),可能对应多个值,所以需要用foreach循环进行遍历。

$FolderLocation="D:\Mooing\Domains" #设置AWS导出的Domain文件夹
$ZoneLocation="D:\Mooing\Zone" #设置转换后的文件夹
$Domains=Get-ChildItem $FolderLocation
foreach ($domain in $Domains)
{
cd $FolderLocation
$DomainName=Get-Content $FolderLocation\$domain -Raw | ConvertFrom-Json
$Records=$DomainName.ResourceRecordSets

foreach ($Record in $Records)
{

if ($Record.ResourceRecords.Count -eq 1)
{
$Record.Name + " " + $Record.ttl + " IN " + $Record.type + " " + $Record.ResourceRecords.Value.replace("\","") |Out-File $ZoneLocation\$domain.Zone.txt -Append utf8
}
else
{
$i=0
while($i -lt $Record.ResourceRecords.Count) {
$Record.Name + " " + $Record.ttl + " IN " + $Record.type + " " + $Record.ResourceRecords.Value[$i].replace("\","") |Out-File $ZoneLocation\$domain.Zone.txt -Append utf8
$i++
}
}
}
}

转换完成之后,即可导入支持Bind格式的服务商,Azure导入方式请参考:

 ​为 Azure 专用 DNS 导入和导出域区域文件 - Azure CLI - Azure DNS | Microsoft Docs​

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK