Skip to content

Instantly share code, notes, and snippets.

@synchrone
Last active August 29, 2015 14:07
Show Gist options
  • Save synchrone/f65251430b8ab7e825fd to your computer and use it in GitHub Desktop.
Save synchrone/f65251430b8ab7e825fd to your computer and use it in GitHub Desktop.
Use Route53 as your dyndns
#!/usr/bin/env python
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 = 'my.domain'
subdomain = '@'
def get_public_ip():
r = requests.get('http://icanhazip.com')
return r.text.rstrip()
fqdn = '%s.%s' % (subdomain, domain) if subdomain != '@'else 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)
#!/bin/bash
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
python $(dirname $0)/dyndns53.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment