python 使用当前IP在amazon area53 DNS服务上添加/更新A记录的Python脚本。 (即更换dyndns)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用当前IP在amazon area53 DNS服务上添加/更新A记录的Python脚本。 (即更换dyndns)相关的知识,希望对你有一定的参考价值。

from area53 import route53
from boto.route53.exception import DNSServerError
import requests
import sys
from datetime import datetime

# Modified from https://markcaudill.me/blog/2012/07/dynamic-route53-dns-updating-with-python/

domain = 'domain.tld'
subdomain = 'subdomain_name'

def get_public_ip():
    r = requests.get('http://icanhazip.com')
    return r.text.rstrip()

fqdn = '%s.%s' % (subdomain, domain)
zone = route53.get_zone(domain)
arec = zone.get_a(fqdn)
new_value = get_public_ip()
datestr = '"Last update %s."' % datetime.utcnow().strftime('%Y-%m-%d %H:%M')

if arec:
        old_value = arec.resource_records[0]

        if old_value == new_value:
                print '%s is current. (%s)' % (fqdn, new_value)
                sys.exit(0)

        print 'Updating %s: %s -> %s' % (fqdn, old_value, new_value)

        try:
                zone.update_a(fqdn, new_value, 900)
                zone.update_txt(fqdn, datestr, 900)

        except DNSServerError:
                # This can happen if the record did not already exist. Let's
                # try to add_a in case that's the case here.
                zone.add_a(fqdn, new_value, 900)
                zone.add_txt(fqdn, datestr, 900)
else:
        zone.add_a(fqdn, new_value, 900)
        zone.add_txt(fqdn, datestr, 900)

以上是关于python 使用当前IP在amazon area53 DNS服务上添加/更新A记录的Python脚本。 (即更换dyndns)的主要内容,如果未能解决你的问题,请参考以下文章

Amazon AWS 使用心得(摸索篇一)

生成Area URL链接

Amazon AWS负载均衡器EC2实例解决ip范围

如何在 Amazon EC2 上获取所有公共 IP 及其实例名称的列表

有没有办法将多个静态 IP 分配给 Amazon Lightsail 实例?

如何为 IP 地址设置 Amazon S3 策略