-
-
Save josephabrahams/567021239f8ce9066a7f to your computer and use it in GitHub Desktop.
Route53 Dynamic DNS Updater
This file contains hidden or 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
#!/usr/bin/env python | |
# Adapted from http://joseph.is/1HPHiOE | |
import sys | |
import inspect | |
from datetime import datetime | |
from syslog import syslog, LOG_ERR | |
from area53 import route53 | |
from boto.route53.exception import DNSServerError | |
from requests import get | |
from requests.exceptions import ConnectionError, HTTPError, Timeout | |
domain = "domain.tld" | |
subdomain = "subdomain" | |
def get_public_ip(): | |
try: | |
args = {"timeout": 5} | |
r = get("http://icanhazip.com", **args) | |
except (ConnectionError, HTTPError, Timeout) as e: | |
print "%s: Error getting public IP" % (filename) | |
syslog(LOG_ERR, "%s: Error getting public IP" % (filename)) | |
sys.exit(1) | |
return r.text.rstrip() | |
filename = inspect.getfile(inspect.currentframe()) | |
new_value = get_public_ip() | |
fqdn = "%s.%s" % (subdomain, domain) | |
datestr = '"Last update: %s"' % datetime.utcnow().strftime('%Y-%m-%d %H:%M') | |
try: | |
zone = route53.get_zone(domain) | |
arec = zone.get_a(fqdn) | |
except DNSServerError: | |
print "Error finding %s" % (fqdn) | |
syslog(LOG_ERR, "%s: Error finding %s" % (filename, fqdn)) | |
sys.exit(1) | |
if arec: | |
old_value = arec.resource_records[0] | |
if old_value == new_value: | |
print "%s is current. (%s)" % (fqdn, new_value) | |
syslog("%s: %s is current. (%s)" % (filename, fqdn, new_value)) | |
sys.exit(0) | |
print "Updating %s: %s -> %s" % (fqdn, old_value, new_value) | |
syslog("%s: Updating %s: %s -> %s" % (filename, fqdn, old_value, new_value)) | |
try: | |
zone.update_a(fqdn, new_value, 300) | |
zone.update_txt(fqdn, datestr, 300) | |
except DNSServerError: | |
print "Error updating %s" % (fqdn) | |
syslog(LOG_ERR, "%s: Error updating %s" % (filename, fqdn)) | |
sys.exit(1) | |
else: | |
print "Error finding %s" % (fqdn) | |
syslog(LOG_ERR, "%s: Error finding %s" % (filename, fqdn)) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment