-
-
Save xinatcg/b81e55407a636ed4e6843c2c1ca85ffe to your computer and use it in GitHub Desktop.
Python script to add/update an A record at amazon area53 DNS service, using current IP. (ie, dyndns replacement)
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment