LoginSignup
1
1

More than 5 years have passed since last update.

Route53に登録されたレコードを全取得するスクリプト

Last updated at Posted at 2018-01-18

実行条件

  • awscliをインストールしておく
  • EC2インスタンスにアクセスキー ID とシークレットアクセスキーを登録する
#route53.py

import commands
import json

MKDIR=commands.getoutput("mkdir RECORD")
HostZone = commands.getoutput("aws route53 list-hosted-zones")

HostZoneJson = json.loads(HostZone)

for h in HostZoneJson["HostedZones"]:
  HostZoneId = h['Id']
  HostZoneName = h['Name']
  File = "RECORD/" + HostZoneName + "txt"
  f = open(File, 'w')

  AwsCli = "aws route53 list-resource-record-sets --hosted-zone-id " +  HostZoneId 
  Record = commands.getoutput(AwsCli)
  RecordJson = json.loads(Record)

  for r in RecordJson['ResourceRecordSets']:
    Name = r.get('Name')
    Type = r.get('Type')
    TTL = r.get('TTL')
    Resouce = r.get('ResourceRecords')
    Alias = r.get('AliasTarget')

    if Alias != None:
      DNSName = Alias.get('DNSName')
      OutPut = Name + "," + Type + "," + "Alias " + DNSName + "," + "60" + "\n"
      f.write(OutPut)  
    else:
      Value = ""

      for v in Resouce:
        Value = Value + " " + v.get('Value') 

      OutPut = Name + "," + Type + "," +  Value + "," + str(TTL) + "\n"
      f.write(OutPut)

実行方法

python route53.py

ホストゾーン毎にファイルが作成され、レコード一覧が各ファイルに記載される

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1